无法设置SelectionChangedCommand.Command

时间:2012-06-15 21:04:37

标签: wpf mvvm telerik prism

我尝试使用此xaml,将事件应用于命令绑定:

 <telerik:RadGridView x:Name="xRadGridView"

                            prismcommands:SelectionChangedCommand.Command="{Binding SelectPersonCommand}"

                             ItemsSource="{Binding GridItems, Mode=TwoWay}">
        </telerik:RadGridView>

我收到错误:

  

'SelectionChangedCommand.Command'属性是只读的,不可以   从标记设置。

我可以绑定到prismcommands:RowEditEndedCommand.Command没有问题。 有没有机会绑定到SelectionChangedCommand.Command?

我在Silverlight项目中使用相同的PrismCommands,它可以在那里工作。

namespace RadEventToCommand.WPF.PrismCommands
{
    public class RowEditEndedCommandBehavior : CommandBehaviorBase<RadGridView>
    {
        public RowEditEndedCommandBehavior(RadGridView gridView)
            : base(gridView)
        {
            gridView.RowEditEnded +=new EventHandler<GridViewRowEditEndedEventArgs>(gridView_RowEditEnded);
        }

        void gridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {
            CommandParameter = e;

            ExecuteCommand();
        }
    }
}

-

namespace RadEventToCommand.WPF.PrismCommands
{
    public static class SelectionChangedCommand
    {
        private static readonly DependencyProperty SelectionChangedCommandBehaviorProperty
            = DependencyProperty.RegisterAttached(
            "SelectionChangedCommandBehavior",
            typeof(SelectionChangedCommandBehavior),
            typeof(SelectionChangedCommand),
            null);

        public static readonly DependencyProperty CommandProperty
            = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(SelectionChangedCommand),
            new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty
            = DependencyProperty.RegisterAttached(
           "CommandParameter",
           typeof(object),
           typeof(SelectionChangedCommand),
           new PropertyMetadata(OnSetCommandParameterCallback));

        public static ICommand GetCommand(RadGridView gridView)
        {
            return gridView.GetValue(CommandProperty) as ICommand;
        }

        public static void SetCommandParameter(RadGridView gridView, object parameter)
        {
            gridView.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(RadGridView gridView)
        {
            return gridView.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            RadGridView gridView = dependencyObject as RadGridView;
            if (gridView != null)
            {
                SelectionChangedCommandBehavior behavior = GetOrCreateBehavior(gridView);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            RadGridView gridView = dependencyObject as RadGridView;
            if (gridView != null)
            {
                SelectionChangedCommandBehavior behavior = GetOrCreateBehavior(gridView);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static SelectionChangedCommandBehavior GetOrCreateBehavior(RadGridView gridView)
        {
            SelectionChangedCommandBehavior behavior =
                gridView.GetValue(SelectionChangedCommandBehaviorProperty) as SelectionChangedCommandBehavior;
            if (behavior == null)
            {
                behavior = new SelectionChangedCommandBehavior(gridView);
                gridView.SetValue(SelectionChangedCommandBehaviorProperty, behavior);
            }
            return behavior;
        }
    }
}

-

namespace RadEventToCommand.WPF.PrismCommands
{
    public class RowEditEndedCommandBehavior : CommandBehaviorBase<RadGridView>
    {
        public RowEditEndedCommandBehavior(RadGridView gridView)
            : base(gridView)
        {
            gridView.RowEditEnded +=new EventHandler<GridViewRowEditEndedEventArgs>(gridView_RowEditEnded);
        }

        void gridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {
            CommandParameter = e;

            ExecuteCommand();
        }
    }

}

-

namespace RadEventToCommand.WPF.PrismCommands
{
    public static class RowEditEndedCommand
    {
        private static DependencyProperty RowEditEndedCommandBehaviorProperty
            = DependencyProperty.RegisterAttached(
            "RowEditEndedCommandBehavior",
            typeof(RowEditEndedCommandBehavior),
            typeof(RowEditEndedCommand),
            null);

        public static DependencyProperty CommandProperty
            = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(RowEditEndedCommand),
            new PropertyMetadata(OnSetCommandCallback));

        public static DependencyProperty CommandParameterProperty
            = DependencyProperty.RegisterAttached(
           "CommandParameter",
           typeof(object),
           typeof(RowEditEndedCommand),
           new PropertyMetadata(OnSetCommandParameterCallback));

        public static ICommand GetCommand(RadGridView gridView)
        {
            return gridView.GetValue(CommandProperty) as ICommand;
        }

        public static void SetCommand(RadGridView gridView, object parameter)
        {
            gridView.SetValue(CommandProperty, parameter);
        }

        public static void SetCommandParameter(RadGridView gridView, object parameter)
        {
            gridView.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(RadGridView gridView)
        {
            return gridView.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            RadGridView gridView = dependencyObject as RadGridView;
            if (gridView != null)
            {
                RowEditEndedCommandBehavior behavior = GetOrCreateBehavior(gridView);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            RadGridView gridView = dependencyObject as RadGridView;
            if (gridView != null)
            {
                RowEditEndedCommandBehavior behavior = GetOrCreateBehavior(gridView);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static RowEditEndedCommandBehavior GetOrCreateBehavior(RadGridView gridView)
        {
            RowEditEndedCommandBehavior behavior =
                gridView.GetValue(RowEditEndedCommandBehaviorProperty) as RowEditEndedCommandBehavior;
            if (behavior == null)
            {
                behavior = new RowEditEndedCommandBehavior(gridView);
                gridView.SetValue(RowEditEndedCommandBehaviorProperty, behavior);
            }
            return behavior;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我有从Silverlight项目复制的行为的来源。它在那里工作。出于某些原因,我在WPF中需要SelectionChangedCommand

中的附加方法
   public static void SetCommand(RadGridView gridView, object parameter)
        {
            gridView.SetValue(CommandProperty, parameter);
        }

我复制了代码以检查我是否可以使用Silverlight和WPF的公共代码库。

答案 1 :(得分:0)

对于RadGridView,我们使用了交互触发器。以下代码适用于我们。

<i:Interaction.Triggers>
   <i:EventTrigger EventName="SelectionChanged">
      <i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"/>
   </i:EventTrigger>
</i:Interaction.Triggers>