I have a DataGridTextViewColumn that I want to restrict input for. I've already attached handlers for PreviewTextInput and PreviewKeyDown events, but I also need to limit input via a Paste command. How do I handle the Paste command for this text column? My attempt is below:
<DataGridTextColumn Binding="{Binding MyProperty, UpdateSourceTrigger=PropertyChanged}"
Width="Auto">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<EventSetter Event="PreviewTextInput"
Handler="MyProperty_PreviewTextInput"/>
<!-- Space, backspace, and delete are not available in PreviewTextInput,
so we have to capture them in the PreviewKeyDown event -->
<EventSetter Event="PreviewKeyDown"
Handler="MyProperty_PreviewKeyDown"/>
<!-- I get a compiler error that "The Property Setter 'CommandBindings'
cannot be set because it does not have an accessible set accessor" -->
<Setter Property="CommandBindings">
<Setter.Value>
<CommandBinding Command="Paste"
Executed="MyProperty_PasteExecuted"/>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
I understand why my attempt doesn't work, I just can't find a solution I'm happy with that does what I want. The only solution I've found so far is this SO post from 2010 (DataGridTextColumn event binding). I'm hoping there's a better solution by now.