Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-将可见性绑定到密钥状态_C#_.net_Wpf_Mvvm_Ivalueconverter - Fatal编程技术网

C# WPF-将可见性绑定到密钥状态

C# WPF-将可见性绑定到密钥状态,c#,.net,wpf,mvvm,ivalueconverter,C#,.net,Wpf,Mvvm,Ivalueconverter,如果可能的话,我需要通过转换器将WPF UserControl上控件的可见性绑定到Alt键的状态。按钮应该只有在按下ALT键时才可见,解决方案不应该集成在代码隐藏文件中,因为我正在使用PRISM/Unity的严格MVVM模式中工作 一个完美的解决方案包括编写一个新的转换器,该转换器能够将键盘键的状态转换为用户控件的Visibly属性,但我对转换器几乎没有经验,自己也无法想出一个解决方案。以下是一个完整的示例 Xaml 解释 控件的可见性通过BooleantVisibilityConverter绑

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


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

以下是一个完整的示例

Xaml 解释 控件的可见性通过
BooleantVisibilityConverter
绑定到布尔属性。 然后我使用两个命令。一个在使用键绑定按下Alt键时触发,第二个在向上键时触发。当出现应该添加的向上键时,我遗漏了对Alt键的检查。如果您想纯粹使用MVVM,这可能会变得很棘手,因为您需要向命令发送一个参数,说明按下的键

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

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
}
已更改命令以获取可为空的键参数

public DelegateCommand<Key?> KeyUnpressedCommand { get; set; }
public DelegateCommand键未组装命令{get;set;}
并加以实施

KeyUnpressedCommand = new DelegateCommand<Key?>(key => 
{
    if (key == Key.LeftAlt)
        IsAltPressed = false;
});
keynupassedCommand=newdelegateCommand(键=>
{
if(key==key.LeftAlt)
IsAltPressed=false;
});

希望这有助于在转换器中使用输入绑定。我会发一封信example@Omribitan那太好了!这看起来是一个很好的解决方案,但我还必须修改视图的视图模型文件,这通常不是我想要的。我有一种轻微的感觉,虽然不会有其他的方式。。。如果我的假设被证明是正确的,我会将你的答案标记为正确的。这通常来自Blend命名空间…你是否安装了Blend?@Klausklapper你需要添加对
System.Windows.Interactivity
的引用。正如@Noctis所说,它通常随
Blend
安装而来,但如果你没有安装,你可以从中获得它。好吧,在附加参考上做得太过分了。我已经安装了blend,但我不太确定这个解决方案是否能满足我们的代码QA:@Omribitan的新更新使我的答案变得多余,但同样,有时候代码隐藏是正确的答案:)(已经安装了,完成了,我知道你的感受)。无论如何,Omri干得好。
<!-- Used behaviors instead of triggers -->
<i:Interaction.Behaviors>
    <local:PreviewKeyUpBehavior Command="{Binding KeyUnpressedCommand}"/>
</i:Interaction.Behaviors>
public DelegateCommand<Key?> KeyUnpressedCommand { get; set; }
KeyUnpressedCommand = new DelegateCommand<Key?>(key => 
{
    if (key == Key.LeftAlt)
        IsAltPressed = false;
});