1

我尝试使用此 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;
        }
    }
}
4

2 回答 2

0

我从 Silverlight 项目中复制了行为的来源。它在那里工作。由于 WPF 中的某些原因,我需要 SelectionChangedCommand 中的附加方法

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

我复制了代码以检查是否可以为 Silverlight 和 WPF 使用通用代码库。

于 2012-06-17T19:42:17.933 回答
0

对于 RadGridView,我们使用交互触发器。下面的代码对我们有用。

<i:Interaction.Triggers>
   <i:EventTrigger EventName="SelectionChanged">
      <i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"/>
   </i:EventTrigger>
</i:Interaction.Triggers>
于 2012-07-31T15:13:44.840 回答