6

I wish to have a custom column type in a WPF datagrid, part of which will be a textbox for user input. Unfortunately, it does not appear to inherit the look and feel of the datagrid itself- it doesn't show the alternating colour, when a row is selected or edited the cell in question doesn't highlight in the same way, and so on.

           <DataGridTemplateColumn Header="Name" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <TextBox Text="{Binding DisplayName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" BorderThickness="0" />                          
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

It looks like the style of a default textbox overrides that of the datagrid; is there a way just to use the datagrids style? I could of course style the textbox to mimic the datagrid, but if I wish to add other controls I'd have to do that for each one as well. If I do go down that route, how would I change the style based on properties of the datagridrow from within the celltemplate?- for example IsSelected.

Ricardo Campos
  • 481
  • 4
  • 9

1 Answers1

0

Please change your XAML to add the following to your textbox definition:

BorderThickness="0"      
Background="{Binding RelativeSource={RelativeSource  AncestorType=DataGridRow}, Path=Background}"

This will make the Textbox inherit your datagrid backround property.

Good Luck

Adi Solar
  • 127
  • 1
  • 1
  • 13