2

I'm learning MVVM and PRISM and trying to handle the Drop and DragEnter events for a TextBox.

I have managed to do this successfully for a button click

    public ButtonsViewModel()
    {
        //If statement is required for viewing the MainWindow in design mode otherwise errors are thrown
        //as the ButtonsViewModel has parameters which only resolve at runtime. I.E. events
        if (!(bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)
        {
            svc = ServiceLocator.Current;
            events = svc.GetInstance<IEventAggregator>();
            events.GetEvent<InputValidStatus>().Subscribe(SetInputStatus);
            StartCommand = new DelegateCommand(ExecuteStart, CanExecute).ObservesProperty(() => InputStatus);
            ExitCommand = new DelegateCommand(ExecuteExit);
        }
    }

    private bool CanExecute()
    {
        return InputStatus;
    }

    private void ExecuteStart()
    {
        InputStatus = true;
        ERA Process = new ERA();
        Proces.Run();
    }

This works fine and have no issues with doing this for other events which do not take EventArgs. So the Drop method will be fine to implement as I don't need to interact with the EventArgs.

However with the Textbox_DragEnter event it sets the DragDropEffects of the TextBox

    private void TextBox_DragEnter(object sender, DragEventArgs e)
    {
        e.Effects = DragDropEffects.Copy;
    }

My first thought was to create a ICommand and bind it to the TextBox_DragEnter event and within the ViewModel have this update a DragDropEffects property. But I cant see how to bind the effect to the textbox.

I may be thinking about this wrong. What is the proper way to do this?

I know that I can set these events easily in the code behind but I would prefer not to do this and keep it purely using the MVVM Pattern

Hope this makes sense.

gheff
  • 183
  • 2
  • 14

3 Answers3

2

Another interaction trigger solution to this, similar to what Kevin proposed, but this will work with Prism (non-MVVMLight solution).

Namespace needed:

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

XAML:

<TextBox Name="TextBox" Text="{Binding MVFieldToBindTo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="DragEnter">
            <i:InvokeCommandAction 
                Command="{Binding BoundCommand}" 
                CommandParameter="{Binding Text, ElementName=TextBox}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

The BoundCommand would be a DelegateCommand in the view model. It looks like you have a good idea what that is already. This is written using DragEnter, but I have only used this for LostFocus events in practice, so you may have to play around with this a bit. It should get you going in the right direction.

R. Richards
  • 24,603
  • 10
  • 64
  • 64
  • Not the way it is written above. Take a look here: http://stackoverflow.com/questions/13565484/how-can-i-pass-the-event-argument-to-a-command-using-triggers – R. Richards Dec 23 '16 at 13:24
  • I used the description given by Sean Chase in this post supplied by R. Richards to solve my problem http://stackoverflow.com/questions/13565484/how-can-i-pass-the-event-argument-to-a-command-using-triggers – gheff Dec 30 '16 at 14:52
0

You can use the interaction event triggers to fire a command in your viewmodel. For example below I am wiring the command X up to the RowActivated event. This uses the MVVMLight EventToCommand helper. Put this code inside of your control

<i:Interaction.Triggers>
      <i:EventTrigger EventName="RowActivated">
           <commands:EventToCommand Command="{Binding X}" PassEventArgsToCommand="True"/>
      </i:EventTrigger>
</i:Interaction.Triggers>

The namespaces you need are

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:commands="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
Kevin Ross
  • 7,185
  • 2
  • 21
  • 27
0

Have a look at GongSolutions.WPF.DragDrop for an easy to use MVVM-able drag and drop framework.

Haukinger
  • 10,420
  • 2
  • 15
  • 28