在GridViewComboBoxColumn WPF中调用OnSelectionChanged命令

时间:2016-01-27 07:40:55

标签: c# wpf telerik telerik-grid

当选择更改发生时,有没有办法可以调用command并将SelectedItem作为参数传递给ViewModel

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>

我尝试添加Interation Triggers,但我无法找到确切的事件来传递SelectedItem作为参数,

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

视图模型:

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

void StatusDropdown()
{

}

请帮助。

更新代码:

<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>

发行Occured:

Issue

1 个答案:

答案 0 :(得分:1)

似乎订阅Selector.SelectionChanged路由事件应该可以完成这项任务。

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

您需要自定义触发器来处理附加事件:

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;
    }
}

您也可以使用自己的操作来触发命令:

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);
                    }
                }
            }
        }
    }
}