none
Command getting null in my Attached property used for converting Event to command in c# wpf (MVVM)

    Question

  •  

    hi
    I am using mvvm so i am trying to have less (or possibly no) code in code behind.
    I want to convert KeyDown event on a TextBox to Command.I am using Interactivity dll
    my Xaml code is:

    <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>
    
    </TextBox>
     and the code for converting EventToCommand is:
    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);
                }
            }
    
        }
    problem is in my Invoke  method I am Getting Command=null..
    Am not getting why so? .Thanx.
    and I want to excecute logic in ExecuteCommand method only if the pressed key in enter or tab.
    In code behind we can do this by using KeyEventArgs.
             KeyEventArgs e;        
               if (e.Key == Key.Enter || e.Key == Key.Tab)
    //logic
     how can I achieve this in ExecuteCommand method???
    Wednesday, August 06, 2014 6:05 AM

Answers

All replies