入力を制限したい DataGridTextViewColumn があります。PreviewTextInput および PreviewKeyDown イベントのハンドラーを既にアタッチしましたが、Paste コマンドによる入力も制限する必要があります。このテキスト列の [貼り付け] コマンドを処理するにはどうすればよいですか? 私の試みは以下です:
<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 event binding ) です。今までにもっと良い解決策があることを願っています。