C# MVVM和Prism-如何在ViewModel中处理TextBox\u DragEnter和TextBox\u Drop事件

C# MVVM和Prism-如何在ViewModel中处理TextBox\u DragEnter和TextBox\u Drop事件,c#,wpf,mvvm,event-handling,prism,C#,Wpf,Mvvm,Event Handling,Prism,我正在学习MVVM和PRISM,并尝试为文本框处理Drop和DragEnter事件 我成功地点击了一个按钮 public ButtonsViewModel() { //If statement is required for viewing the MainWindow in design mode otherwise errors are thrown //as the ButtonsViewModel has parameters which

我正在学习MVVM和PRISM,并尝试为文本框处理Drop和DragEnter事件

我成功地点击了一个按钮

    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();
    }
我的第一个想法是创建一个ICommand并将其绑定到TextBox_DragEnter事件,并在ViewModel中使用DragDropEffects属性进行此更新。但是我看不出如何将效果绑定到文本框

我可能想错了。正确的方法是什么

我知道我可以在代码隐藏中轻松地设置这些事件,但我不希望这样做,而是完全使用MVVM模式来保持它


希望这有意义。

您可以使用交互事件触发器在viewmodel中触发命令。例如,下面我将命令X连接到RowActivated事件。这将使用MVVMLight EventToCommand帮助程序。将此代码置于您的控件内

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

另一个交互触发解决方案类似于Kevin提出的,但这将与Prism(非MVVMLight解决方案)一起使用

所需命名空间:

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



BoundCommand将是视图模型中的DelegateCommand。看起来你已经有了一个好主意。这是使用DragEnter编写的,但我在实践中仅将其用于LostFocus事件,因此您可能需要稍微处理一下。它应该会让您朝着正确的方向前进。

看看一个易于使用的MVVM拖放框架。

我想我向事件添加了属性,然后将这些属性中的事件参数传递给ViewModel。您是如何从视图中执行此操作的?是的,我没有找到更好的解决方案,不是上面写的方式。看看这里:我用R.Richards提供的Sean Chase在这篇文章中给出的描述来解决我的问题
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:commands="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<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>