我使用MVVM
模式开发应用程序。我使用MVVMLight库来做到这一点。所以,如果我需要处理TextBox
TextChange
事件我在XAML写:Wpf MVVM如何在ViewModel中处理TextBox“粘贴事件”
<I:EventTrigger EventName="TextChanged">
<I:InvokeCommandAction Command="{Binding PropertyGridTextChange}"/>
</I:EventTrigger>
其中PropertyGridTextChange
是ViewModel
Command
。但TextBox
没有Paste
活动!如果应用程序不使用MVVM
模式,因为你需要有TextBox
链接
This解决方案仅适用。
<DataTemplate x:Key="StringTemplate">
<TextBox Text="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</TextBox>
</DataTemplate>
重要的细节 - TextBox
放置在DataTemplate
。 我不知道如何处理“粘贴事件”。 当我将文本粘贴到TextBox
时,我想要调用PasteCommand
。我需要TextBox.Text
或TextBox
本身作为参数传递到PasteCommandMethod
。
private RelayCommand<Object> _pasteCommand;
public RelayCommand<Object> PasteCommand
{
get
{
return _pasteCommand ?? (_pasteCommand =
new RelayCommand<Object>(PasteCommandMethod));
}
}
private void PasteCommandMethod(Object obj)
{
}
如果粘贴文本,您希望视图模型做什么? – Dirk 2015-02-05 14:40:28
我想要一些'Command'被调用。 – monstr 2015-02-05 14:42:43
好的,我应该更准确地说出这个问题:为什么你特别想处理复制和粘贴事件,而不是仅仅绑定Text属性的数据? – Dirk 2015-02-05 14:45:29