如何根据 DataGridColumn 中的信息设置 DataGridHeader 的样式?
我有一个 DataGrid,在该网格中,某些列被标记为只读:
<DataGrid AutoGenerateColumns="False">
<DataGrid.Columns>
<!-- this column is read only -->
<DataGridTextColumn Header="Column A" Binding="{Binding Path=PropertyA}" IsReadOnly="True" />
<!-- this column is EDITABLE -->
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" />
<!-- this column is read only -->
<DataGridTextColumn Header="Column C" Binding="{Binding Path=PropertyC}" IsReadOnly="True" />
</DataGrid.Columns>
我希望“名称”列在视觉上可以通过标题来区分,因为它是可编辑的,而其他两列则不可编辑。不过,我似乎无法访问 DataGridColumn 的 IsReadOnly 属性。
我实际上正在尝试做类似的事情:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader" >
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumn}, Path=IsReadOnly}" Value="false">
<Setter Property="Background" Value="Azure" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.ColumnHeaderStyle>
从这个问题: 在 WPF DataGrid 中绑定 DataGridColumn 的 Visible 属性,显示DataGridColumn 不是框架元素,因此我无法使用 RelativeSource AncestorType=DataGridColumn
找到它。该海报说他们使用静态资源来找到它,但没有解释什么/如何(以及几个答案,其中有关于海报如何解决它的问题)
这个问题: 如何从 DataGridColumn 获取 DataGridColumnHeader?,看起来我可以从代码获取它,但我会我真的很喜欢它只是 xaml 和通用的,适用于任何数据网格。
我忽略了一些简单的事情吗?
I have a DataGrid, and in that grid, some of the columns are marked Read only:
<DataGrid AutoGenerateColumns="False">
<DataGrid.Columns>
<!-- this column is read only -->
<DataGridTextColumn Header="Column A" Binding="{Binding Path=PropertyA}" IsReadOnly="True" />
<!-- this column is EDITABLE -->
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" />
<!-- this column is read only -->
<DataGridTextColumn Header="Column C" Binding="{Binding Path=PropertyC}" IsReadOnly="True" />
</DataGrid.Columns>
I want that "Name" column to be visually distinguishable by the header that it is editable, when the other two columns are not. I can't seeem to get to the IsReadOnly property of the DataGridColumn, though.
I'm effectively trying to do something like:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader" >
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumn}, Path=IsReadOnly}" Value="false">
<Setter Property="Background" Value="Azure" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.ColumnHeaderStyle>
From this question:
Binding Visible property of a DataGridColumn in WPF DataGrid, it appears that DataGridColumn isn't a framework element, so i can't find it using RelativeSource AncestorType=DataGridColumn
. That poster says they used a static resource to find it, but doesn't explain what/how (and several answers there are questions of how the poster solved it)
This question: How to get DataGridColumnHeader from DataGridColumn?, looks like i could get to it from code, but i'd really like this to just be xaml and generic to apply to any data grid.
Is there something simple i'm overlooking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答我自己的问题,以防其他人遇到这个问题...
事实证明,由于问题中提到的一些事情,您无法通过 XAML 在这里做很多事情。我可以从 XAML 做的最简单的事情就是将可编辑列的标题设为粗体,以将它们与其余列区分开来。
您可以做更复杂的事情,例如:
但是您最终会得到一些非常奇怪的行为,排序图标消失,或其他奇怪的事情。如果您想更改列标题并使其看起来一致,您几乎必须通过样式和模板重新设置整个数据网格及其所有标题的样式。
Answering my own question, in case anyone else runs into this...
It turns out there isn't a lot you can do here from XAML, because of some of the things mentioned in the question. The simplest thing i could do from XAML was make the headers of the editable columns bold to differentiate them from the rest of the columns.
You can do more complicated things, like:
But you end up getting some really wierd behaviors, where the sort icon disappears, or other wierd things. If you want to change the column headers and make it look consistant, you pretty much have to restyle the whole datagrid and all of its headers through styles and templates.
DataGridColumnHeader 具有属性 Column,该属性具有属性 IsReadOnly
在您的示例中,您可以执行以下操作:
DataGridColumnHeader has property Column, which has the property IsReadOnly
In your example you could do: