我希望在 WPF 数据网格中有一个自定义列类型,其中一部分将是用于用户输入的文本框。 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>
看起来默认文本框的样式覆盖了数据网格的样式;有没有办法只使用数据网格样式?我当然可以设置文本框的样式以模仿数据网格,但是如果我想添加其他控件,我也必须为每个控件都这样做。如果我确实沿着那条路线走,我将如何根据单元格模板中数据网格的属性更改样式? - 例如 IsSelected。
最佳答案
请更改您的 XAML 以将以下内容添加到您的文本框定义中:
BorderThickness="0"
Background="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=Background}"
这将使文本框继承您的数据网格背景属性。
祝你好运
关于wpf - 在自定义 CellTemplate 中继承 WPF DataGrid 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9722286/