我有一个要限制输入的 DataGridTextViewColumn。我已经为 PreviewTextInput 和 PreviewKeyDown 事件附加了处理程序,但我还需要通过粘贴命令限制输入。如何处理此文本列的粘贴命令?我的尝试如下:
<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>
我理解为什么我的尝试不起作用,我只是找不到一个我满意的解决方案来满足我的需求。到目前为止,我发现的唯一解决方案是 2010 年的这篇 SO 帖子(DataGridTextColumn 事件绑定)。我希望现在有更好的解决方案。