Search code examples
c#wpfteleriktelerik-grid

Invoke command OnSelectionChanged in GridViewComboBoxColumn WPF


Is there any way that i can invoke a command and pass the SelectedItem as parameter to ViewModel when the selection change occurs?

XAML:

<telerik:GridViewComboBoxColumn ItemsSource="{Binding StatusList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValueMemberPath="StatusName" DisplayMemberPath="StatusName" DataMemberBinding="{Binding Shipped, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsVisible="{Binding IsExist, Mode=TwoWay}">
</telerik:GridViewComboBoxColumn>

I tried like adding Interation Triggers but i couldn't able to find the exact event to pass the SelectedItem as parameter,

<i:Interaction.Triggers>
    <i:EventTrigger EventName="ContextMenuClosing">
        <i:InvokeCommandAction Command="{Binding StatusDropdownCommand, Mode=OneWay}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

ViewModel:

public ICommand StatusDropdownCommand { get { return new RelayCommand(StatusDropdown); } }

void StatusDropdown()
{

}

Kindly help.

Updated Code:

<telerik:GridViewComboBoxColumn ItemsSource="{Binding StatusList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValueMemberPath="StatusName" DisplayMemberPath="StatusName" DataMemberBinding="{Binding Shipped, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsVisible="{Binding IsExist, Mode=TwoWay}">
<i:Interaction.Triggers>
<Converter:RoutedEventTrigger RoutedEvent="Selector.SelectionChanged" >
<Converter:CustomCommandAction Command="{Binding SelectionChangedCommand}" />
</Converter:RoutedEventTrigger>
</i:Interaction.Triggers>
</telerik:GridViewComboBoxColumn>

Issue Occured:

Issue


Solution

  • Seems that subscription to Selector.SelectionChanged routed event should do the job.

    <i:Interaction.Triggers>
            <local:RoutedEventTrigger RoutedEvent="Selector.SelectionChanged">
                <local:CustomCommandAction Command="{Binding SelectionChangedCommand}" />
            </local:RoutedEventTrigger>
    </i:Interaction.Triggers>
    

    You need custom trigger to handle attached events:

    public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
    {
        RoutedEvent _routedEvent;
    
        public RoutedEvent RoutedEvent
        {
            get { return _routedEvent; }
            set { _routedEvent = value; }
        }
    
        public RoutedEventTrigger()
        {
        }
        protected override void OnAttached()
        {
            Behavior behavior = base.AssociatedObject as Behavior;
            FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement;
    
            if (behavior != null)
            {
                associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
            }
            if (associatedElement == null)
            {
                throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
            }
            if (RoutedEvent != null)
            {
                associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent));
            }
        }
        void OnRoutedEvent(object sender, RoutedEventArgs args)
        {
            base.OnEvent(args);
        }
        protected override string GetEventName()
        {
            return RoutedEvent.Name;
        }
    }
    

    Also you may use your own action for triggering your command:

    public sealed class CustomCommandAction : TriggerAction<DependencyObject>
    {
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(CustomCommandAction), null);
    
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
            "Command", typeof(ICommand), typeof(CustomCommandAction), null);
    
        public ICommand Command
        {
            get
            {
                return (ICommand)this.GetValue(CommandProperty);
            }
            set
            {
                this.SetValue(CommandProperty, value);
            }
        }
    
        public object CommandParameter
        {
            get
            {
                return this.GetValue(CommandParameterProperty);
            }
    
            set
            {
                this.SetValue(CommandParameterProperty, value);
            }
        }
    
        protected override void Invoke(object parameter)
        {
            if (this.AssociatedObject != null)
            {
                ICommand command = this.Command;
                if (command != null)
                {
                    if (this.CommandParameter != null)
                    {
                        if (command.CanExecute(this.CommandParameter))
                        {
                            command.Execute(this.CommandParameter);
                        }
                    }
                    else
                    {
                        if (command.CanExecute(parameter))
                        {
                            command.Execute(parameter);
                        }
                    }
                }
            }
        }
    }