ICommand



  • For the time being, this is the approach used to obtain the mouse coordinates:

    XAML

    <interactivity:Interaction.Triggers>
            <interactivity:EventTrigger EventName="PreviewDragOver">
                <i:CallMethodAction MethodName="UIElement_OnPreviewDragOver" TargetObject="{Binding}" />
            </interactivity:EventTrigger>
    </interactivity:Interaction.Triggers>
    

    VM

    public void UIElement_OnPreviewDragOver(object sender, DragEventArgs e)
    {
        Point = e.GetPosition((TreeView)sender);
    }
    

    I don't like what I'm inside. VM I'm working with the counter, and I'd like to redo this processor. ICommand♪ But how do you get the mouse coordinates?



  • Because the mouse coordinates aren't so easy to connect through XAML, it's easier to make a code-behind. Let your VM team lie in its own right. DragCommand

    In XAML:

    <Grid PreviewDragOver="OnPreviewDragOver" ...>
    

    The code-behind has to do this:

    We'll dependency property so we don't cast. DataContext type VM

    public ICommand DragPreviewCommand
    {
        get { return (ICommand)GetValue(DragPreviewCommandProperty); }
        set { SetValue(DragPreviewCommandProperty, value); }
    }
    

    public static readonly DependencyProperty DragPreviewCommandProperty =
    DependencyProperty.Register("DragPreviewCommand", typeof(ICommand), typeof(MyWindow));

    The designer shall be connected (in XAML, not possible):

    public MyWindow()
    {
    InitializeComponent();
    SetBinding(DragPreviewCommandProperty, "DragCommand");
    }

    And define the processor:

    void OnPreviewDragOver(object sender, DragEventArgs e)
    {
    var senderUIElement = (UIElement)sender;
    var mousePosition = e.GetPosition(senderUIElement);
    var command = DragPreviewCommand;
    if (command != null)
    command.Execute(mousePosition);
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2