I develop application with using MVVM
pattern. I using MVVMLight library to do this. So if I need to handle TextBox
TextChange
event I write in XAML:
<I:EventTrigger EventName="TextChanged">
<I:InvokeCommandAction Command="{Binding PropertyGridTextChange}"/>
</I:EventTrigger>
where PropertyGridTextChange
is Command
in ViewModel
. But TextBox
has no Paste
event!
This solution only works if application don't use MVVM
pattern, because you need to have link on TextBox
.
<DataTemplate x:Key="StringTemplate">
<TextBox Text="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</TextBox>
</DataTemplate>
Important detail - TextBox
placed within DataTemplate
.
I have no idea how can I handle "paste event".
I want PasteCommand
to be invoked when I paste text into TextBox
. And I need that TextBox.Text
or TextBox
itself to be passed as parameter into PasteCommandMethod
.
private RelayCommand<Object> _pasteCommand;
public RelayCommand<Object> PasteCommand
{
get
{
return _pasteCommand ?? (_pasteCommand =
new RelayCommand<Object>(PasteCommandMethod));
}
}
private void PasteCommandMethod(Object obj)
{
}
I've been struggling with this kind of problem too in recent days. My first approach would be to have a property in the VM that is bound to the text box (which I am sure you already have). Then bind an ICommand to an event to handle the on paste event:
you need to define the namespace in the proper part of the XAML code, and then put the interaction triggers in as part of the textbox definition. Here I am capturing the RowEditEnding event to do some stuff similar to what you are attempting.
The command binding is another piece, let me know if you need more information on how that needs to be set up.
I can suggest answer on my question.
Class-helper.
Using in XAML. In
TextBox
as attribute.PropertyGridTextPasted
- Command in theViewModel
.