Inheriting WPF DataGrid Styles in a Custom CellTemplate Cell

I want to have my own column type in datagrid WPF, part of which will be a text box for user input. Unfortunately, it does not seem to inherit the appearance of the datagrid itself - it does not display alternating color when the row is selected or the cell in question is edited, it does not stand out in the same way, etc.

<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 default text box style overrides the datagrid file style; is there any way to use datagrids style? I could, of course, create a text box to mimic a datagrid, but if I want to add other controls, I have to do this for each one as well. If I share along this route, how would I change the style based on the datagridrow properties from inside the celltemplate? - for example IsSelected.

+4
source share
1 answer

Modify your XAML to add the following to the text field definition:

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

This will cause the text box to inherit your backatrid backround property.

Luck

0
source

Source: https://habr.com/ru/post/1401626/


All Articles