0

次のように、テキスト ボックスで MouseEnter などのイベントでカスタム ICommand を呼び出すことができるように、WPF に Julmar ヘルパー クラスを使用しています。

<TextBox Text="hmm">
    <julmar:EventCommander.Mappings>    
        <julmar:CommandEvent Command="{Binding DataContext.IncreaseQueueTimeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" Event="MouseEnter" />
    </julmar:EventCommander.Mappings>
</TextBox>

これは機能し、コマンドを呼び出します。問題は、オブジェクトをパラメーターとして渡す必要があることです。これが可能かどうかは誰にもわかりませんか? ドキュメントはかなり軽いようです。

以前は、次のようにオブジェクトをパラメーターとして渡すことができました。

<Button Content="Save" x:Name="SaveQueueTimeButton" Command="{Binding DataContext.SaveQueueTimeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding}" />

しかし、明らかに、これは MouseEvent で起動しないため、私が必要としているものではありません

どんな助けでも役に立ちます、

ありがとう

4

2 に答える 2

1

これは、Behavior-Pattern で解決できます。基本的に、Command と CommandParameter の 2 つの依存関係プロパティを持つカスタム クラスを作成します。また、両方のプロパティのハンドラーを登録します。

ハンドラーの 1 つで、TextBox がパラメーターとして渡されます。これで、関心のあるイベントに接続できます。登録されたイベント ハンドラーのいずれかが呼び出された場合は、バインドされたコマンド パラメーターを使用してバインドされたコマンドを呼び出すことができます。

コードサンプルは次のとおりです。

public class CommandHelper
{
    //
    // Attached Properties
    //
    public static ICommand GetCommand(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(CommandProperty);
    }

    public static void SetCommand(DependencyObject obj, ICommand value)
    {
        obj.SetValue(CommandProperty, value);
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(CommandHelper), new UIPropertyMetadata(null));



    public static object GetCommandParameter(DependencyObject obj)
    {
        return (object)obj.GetValue(CommandParameterProperty);
    }

    public static void SetCommandParameter(DependencyObject obj, object value)
    {
        obj.SetValue(CommandParameterProperty, value);
    }

    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(CommandHelper), new UIPropertyMetadata(null));


    //
    // This property is basically only there to attach handlers to the control that will be the command source
    //
    public static bool GetIsCommandSource(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsCommandSourceProperty);
    }

    public static void SetIsCommandSource(DependencyObject obj, bool value)
    {
        obj.SetValue(IsCommandSourceProperty, value);
    }

    public static readonly DependencyProperty IsCommandSourceProperty =
        DependencyProperty.RegisterAttached("IsCommandSource", typeof(bool), typeof(CommandHelper), new UIPropertyMetadata(false, OnRegisterHandler));


    //
    // Here you can register handlers for the events, where you want to invoke your command
    //
    private static void OnRegisterHandler(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement source = obj as FrameworkElement;

        source.MouseEnter += OnMouseEnter;
    }

    private static void OnMouseEnter(object sender, MouseEventArgs e)
    {
        DependencyObject source = sender as DependencyObject;
        ICommand command = GetCommand(source);
        object commandParameter = GetCommandParameter(source);

        // Invoke the command
        if (command.CanExecute(commandParameter))
            command.Execute(commandParameter);
    }
}

Xaml:

  <TextBox Text="My Command Source"
             local:CommandHelper.IsCommandSource="true"
             local:CommandHelper.Command="{Binding MyCommand}"
             local:CommandHelper.CommandParameter="MyParameter" />
于 2009-06-27T16:13:44.977 に答える