1

入力を制限したい 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 ) です。今までにもっと良い解決策があることを願っています。

4

1 に答える 1

2

errorCommandBindingsプロパティから明らかなようdoesn't have setterに、Style から設定することはできませんが、CommandBindings をinline element in TextBox.

内部的に、CommandBindings.Add()textBox でインラインで宣言すると、xaml はプロパティを呼び出します。したがって、回避策として、 と を使用しDataGridTemplateColumnて提供し、 の外観を与えることができます。それの小さなサンプル -CellTemplateCellEditingTemplateDataGridTextColumn

      <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding MyProperty}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding MyProperty}"
                                 PreviewTextInput="MyProperty_PreviewTextInput"
                                 PreviewKeyDown="MyProperty_PreviewKeyDown">
                            <TextBox.CommandBindings>
                                <CommandBinding Command="Paste" 
                                    Executed="CommandBinding_Executed"/>
                            </TextBox.CommandBindings>
                        </TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
于 2013-04-13T20:54:26.067 に答える