6

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

看起来默认文本框的样式覆盖了数据网格的样式;有没有办法只使用数据网格样式?我当然可以设置文本框的样式来模仿数据网格,但是如果我想添加其他控件,我也必须为每个控件都这样做。如果我确实走这条路,我将如何根据单元格模板中的 datagridrow 的属性更改样式?-例如 IsSelected。

4

1 回答 1

0

请更改您的 XAML 以将以下内容添加到您的文本框定义中:

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

这将使文本框继承您的数据网格背景属性。

祝你好运

于 2014-04-06T10:58:32.413 回答