Brandon Dybala
Brandon Dybala

Reputation: 324

Handle Paste command on DataGridTextColumn

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.

Upvotes: 1

Views: 1722

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81243

As evident from the error CommandBindings property doesn't have setter, so you can't set it from Style but you can do it if you declare CommandBindings as an inline element in TextBox.

Internally, xaml calls CommandBindings.Add() on property if you declare it inline in textBox. So, as a workaround you can use DataGridTemplateColumn and provide a CellTemplate and CellEditingTemplate to give it a look of DataGridTextColumn. Small sample of it -

      <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>

Upvotes: 2

Related Questions