wpf - 在自定义 CellTemplate 中继承 WPF DataGrid 样式

标签 wpf xaml datagrid celltemplate

我希望在 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/

相关文章:

silverlight - 如何将 DataGrid 分组显示更改为其他聚合

c# - 在 WPF 中放大图像

wpf - ListViewItem 自定义模板 : ContentPresenter stays empty

c# - 如何使用 Rg.Plugins.Popup 将 XAML 和模板合并到一个 C# 模板中?

c# - 如何以编程方式将数据添加到 WPF 数据网格

c# - 当 DataGrid 嵌套在分组的 DataGrid 中时,相对列宽不起作用

c# - 绑定(bind)中的 WPF StringFormat 在代码隐藏中不起作用

c# - WPF 新增功能 - 更新代码中的 TextBlock 导致 NullReferenceException

WPF ScrollViewer调整大小问题

c# - StringFormat 在设计器中显示错误,但在编译后不显示