Inheriting WPF DataGrid styles in a custom CellTemplate
Asked Answered
E

1

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.

Evers answered 15/3, 2012 at 14:53 Comment(1)
I have the same problem.I hope someone answer this question:)Luellaluelle
S
1

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

Sting answered 6/4, 2014 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.