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

RadGridViewには、インタラクショントリガーを使用しています。以下のコードは私たちのために働きます。

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

動作のソースを Silverlight プロジェクトからコピーしました。それはそこで働いた。WPFで何らかの理由で、SelectionChangedCommandに追加のメソッドが必要です

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

コードをコピーして、Silverlight と WPF に共通のコードベースを使用できるかどうかを確認しました。

于 2012-06-17T19:42:17.933 に答える