Thumb Drag in MVVM, WPF, C #

I have one working application written in C # WPF. I like to practice working with MVVM, so I like to write the same application as MVVM. But I ran into a problem. In my application I use

<Thumb DragDelta="Thumb_Drag" 
       DragStarted="Thumb_DragStarted"
       DragCompleted="Thumb_DragCompleted"
       ...

How do I write this Drag & Drop / Draggable example in MVVM? Am I using "binding" or something else? Any example would be very welcome :) Thanks!

If you do not understand, ask me.

+4
source share
2 answers

You can use the library System.Windows.Interactivity, which can be added as a link in your project.

Add a namespace:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

And call the commands through <i:InvokeCommandAction>:

<Thumb>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="DragDelta">
            <i:InvokeCommandAction Command="{Binding DataContext.ThumbDragDeltaCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Thumb>

ViewModel:

private ICommand ThumbDragDeltaCommand { get; set;}

public MainViewModel() //Constructor
{
    ThumbDragDeltaCommand = new DelegateCommand(x => 
    {
        //EventHandler
        //Do stuff...
    }
}

, , MVVM, .

+4

, , , . MVVM - , .
, dll System.Windows.Interactivity InvokeCommandAction Interaction.Trigger.

<Thumb>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Thumb_Drag" >
            <i:InvokeCommandAction Command="{Binding SomeViewModelCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Thumb>
+3

Source: https://habr.com/ru/post/1599095/


All Articles