无法设置SelectionChangedCommand.Command

问题描述:

我尝试使用此XAML,申请一个事件,命令绑定:无法设置SelectionChangedCommand.Command

<telerik:RadGridView x:Name="xRadGridView" 

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

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

我得到的错误:

'SelectionChangedCommand.Command' property is read-only and cannot be set from markup.

我可以绑定到prismcommands:RowEditEndedCommand 。命令没有问题。 是否有机会绑定到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; 
     } 
    } 
} 

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

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

额外的方法我在复制代码来检查,如果我能使用Silverlight和WPF一个共同的代码库。

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

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