首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF -将可见性绑定到键的状态

WPF -将可见性绑定到键的状态
EN

Stack Overflow用户
提问于 2013-11-04 09:20:30
回答 1查看 2K关注 0票数 2

我需要将WPF UserControl上的控件的可见性绑定到Alt键的状态,如果可能的话,可以通过转换器。只有当按下ALT键时,Button才是可见的,解决方案不应该集成在Code中,因为我正在使用PRISM/Unity在严格的MVVM模式中工作。

一个完美的解决方案将包括编写一个新的转换器,它可以将键盘键的状态转换为用户控件的可视属性,但我对转换器几乎没有经验,无法自己想出解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-04 09:46:14

下面是一个完整的例子

Xaml

代码语言:javascript
复制
<Window x:Class="Playground.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Playground"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="MainWindow" Height="350" Width="525">
    <Window.InputBindings>
        <KeyBinding Modifiers="Alt" Key="LeftAlt" Command="{Binding AltPressedCommand}" />
    </Window.InputBindings>
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter"/>
    </Window.Resources>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="PreviewKeyUp">
            <i:InvokeCommandAction Command="{Binding AltUnpressedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <Button Content="My Button" Visibility="{Binding IsAltPressed, Converter={StaticResource boolToVisibilityConverter}}"/>
    </Grid>
</Window>

ViewModel

代码语言:javascript
复制
public class MainWindowViewModel : NotificationObject
{
    public MainWindowViewModel()
    {
        AltPressedCommand = new DelegateCommand(() => IsAltPressed = true);
        AltUnpressedCommand = new DelegateCommand(() => IsAltPressed = false);
    }

    public DelegateCommand AltPressedCommand { get; set; }

    public DelegateCommand AltUnpressedCommand { get; set; }

    private bool _IsAltPressed;
    public bool IsAltPressed
    {
        get { return _IsAltPressed; }
        set
        {
            if (value != _IsAltPressed)
            {
                _IsAltPressed = value;
                RaisePropertyChanged("IsAltPressed");
            }
        }
    }
}

解释

控件的可见性通过BooleanToVisibilityConverter绑定到布尔属性。然后我使用两个命令。一个是在使用KeyBinding按下Alt键时触发的,另一个是在键向上时触发的。当出现应添加的向上键时,我忽略了对Alt键的检查。如果您只想使用MVVM,这可能会变得很棘手,因为您需要向命令发送一个参数,说明按下的键。

编辑

我使用以下行为从PreviewKeyUp事件传递键参数

代码语言:javascript
复制
public class PreviewKeyUpBehavior : Behavior<UIElement>
{
    #region Properties

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(PeviewKeyUpBehavior));

    #endregion

    #region Methods

    protected override void OnAttached()
    {
        AssociatedObject.PreviewKeyUp += OnPreviewKeyUp;
        base.OnAttached();
    }

    private void OnPreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (Command == null) return;


        // Execute command and send the key as the command parameter
        Command.Execute(e.Key == Key.System ? e.SystemKey : e.Key);
    } 

    #endregion
}

这将在触发PreviewKeyUp时引发绑定命令,并将键作为命令参数发送。然后,我将视图和ViewModel中的代码修改如下:

代码语言:javascript
复制
<!-- Used behaviors instead of triggers -->
<i:Interaction.Behaviors>
    <local:PreviewKeyUpBehavior Command="{Binding KeyUnpressedCommand}"/>
</i:Interaction.Behaviors>

将命令更改为接受可空键参数。

代码语言:javascript
复制
public DelegateCommand<Key?> KeyUnpressedCommand { get; set; }

并付诸实施

代码语言:javascript
复制
KeyUnpressedCommand = new DelegateCommand<Key?>(key => 
{
    if (key == Key.LeftAlt)
        IsAltPressed = false;
});

希望这能有所帮助

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19764661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档