【问题标题】:Command getting null in my Attached property used for converting Event to command命令在用于将事件转换为命令的附加属性中为空
【发布时间】:2014-08-06 07:27:36
【问题描述】:

我正在使用 mvvm,所以我试图在后面的代码中减少(或可能没有)代码。 我想将 TextBox 上的 KeyDown 事件转换为 Command。我正在使用 Interactivity dll 我的 Xaml 代码是:

  <TextBox Background="White" Name="txtHigh" AcceptsReturn="False" Height="23" Width="98"  Margin="3" Grid.Column="3" Text="{Binding SelectionHigh, Mode=TwoWay,Converter={StaticResource formatCell}}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="KeyDown">
                            <commands:EventToCommand Command="{Binding  HighValueChangedCmd}" ></commands:EventToCommand>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>


而转换EventToCommand的代码是:

  public class EventToCommand : TriggerAction<FrameworkElement>
{
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(EventToCommand), new UIPropertyMetadata(null));


    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventToCommand), new UIPropertyMetadata(null));

    protected override void Invoke(object parameter)
    {
        if (Command == null) return;
        if (Command is RoutedCommand)
        {
            var rc = Command as RoutedCommand;
            if (rc.CanExecute(CommandParameter, base.AssociatedObject))
            {
                rc.Execute(CommandParameter, base.AssociatedObject);
            }
        }
        else
        {
            if (Command.CanExecute(CommandParameter))
                Command.Execute(CommandParameter);
        }
    }

}

问题出在我的 Invoke 方法中,我正在获取 Command=null..我不明白为什么会这样? 并且我只想在 Enter 或选项卡中按下键时才在 ExecuteCommand 方法中执行逻辑。 在后面的代码中,我们可以使用 KeyEventArgs 来做到这一点。

               KeyEventArgs e;        
               if (e.Key == Key.Enter || e.Key == Key.Tab)
              //logic

如何在 ExecuteCommand 方法中实现这一点???

【问题讨论】:

  • 我从标题中删除了标签,should questions include tags in their titles?
  • 愚蠢的问题,但是DataContext中肯定存在HighValueChangedCmd?没有错别字?
  • 是的,它在数据上下文中。你有什么解决办法请贴
  • 复制并粘贴,工作正常。命令不会为空,并且符合预期。能否请您出示您的命令代码
  • 我认为问题不在 .cs 文件中。它在你的绑定中!! Den 在 Binding of Xaml 中给出的任何内容都尝试正确。

标签: c# wpf mvvm


【解决方案1】:

也许您应该尝试另一种方法,不实现 Interaction 和 EventToCommand。

<TextBox Background="White" Name="txtHigh" AcceptsReturn="False" Height="23" Width="98"  Margin="3" Grid.Column="3" Text="{Binding SelectionHigh, Mode=TwoWay,Converter={StaticResource formatCell}}">
 <TextBox.InputBindings>
        <KeyBinding Key="Enter"  Command="{Binding DataContext.HighValueChangedCmd, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" />
        <KeyBinding Key="Tab"  Command="{Binding DataContext.HighValueChangedCmd, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" />
 </TextBox.InputBindings>
</TextBox>

【讨论】:

  • HighValueChanged 命令是写在 ViewModel 中的 bt 控件不在 ViewModel 中????所以它不起作用...我的绑定正确吗???
  • @NehaBalkawade 刚刚编辑了我的答案,尝试在命令绑定中使用 RelativeSource,x:Type 是一种具有包含命令的视图模型的元素(包含文本框的元素)
猜你喜欢
  • 2021-07-25
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 2018-04-02
相关资源
最近更新 更多