【发布时间】:2016-12-23 11:34:44
【问题描述】:
我正在学习 MVVM 和 PRISM,并尝试处理 TextBox 的 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();
}
这可以正常工作,并且对于不采用 EventArgs 的其他事件执行此操作没有问题。所以 Drop 方法可以很好地实现,因为我不需要与 EventArgs 交互。
然而,通过 Textbox_DragEnter 事件,它会设置 TextBox 的 DragDropEffects
private void TextBox_DragEnter(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Copy;
}
我的第一个想法是创建一个 ICommand 并将其绑定到 TextBox_DragEnter 事件,并在 ViewModel 中为此更新 DragDropEffects 属性。但我看不到如何将效果绑定到文本框。
我可能想错了。这样做的正确方法是什么?
我知道我可以在后面的代码中轻松设置这些事件,但我不希望这样做,而是纯粹使用 MVVM 模式来保持它
希望这是有道理的。
【问题讨论】:
-
我认为是给事件添加了属性,然后将这些属性中的事件参数传递给了ViewModel
-
你是怎么做到的?
-
是的,我没有找到更好的解决方案
标签: c# wpf mvvm event-handling prism