gpt4 book ai didi

wpf - 为什么 ItemsPresenter 会覆盖我的 DataGrid 的前景样式?

转载 作者:行者123 更新时间:2023-12-04 06:56:00 24 4
gpt4 key购买 nike

我在 App.xaml 中有一个 DataGrid 样式:

<Style TargetType="{x:Type DataGrid}">
<Setter Property="Foreground" Value="{StaticResource DataGridItemTextBrush}" />
<Setter Property="VerticalGridLinesBrush" Value="{StaticResource GridBrush}" />
<Setter Property="HorizontalGridLinesBrush" Value="{StaticResource GridBrush}" />
<Setter Property="RowBackground" Value="Transparent" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="HeadersVisibility" Value="Column" />
<Setter Property="AlternatingRowBackground" Value="#77000000" />
</Style>

这对我的应用程序中的所有数据网格都非常有用。
但是,对于我的一个数据网格,如果特定列共享相同的值,我想对我的行进行分组。所以我在那个特定的数据网格上使用以下内容:
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" Padding="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="White" />
</Style>
</Expander.Resources>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name, StringFormat=Set: {0}}" Margin="5,0"/>
<TextBlock Text="{Binding Path=ItemCount, StringFormat=(\{0\} Games)}"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>

问题:现在这个 DataGrid 根据我的 DataGrid 样式正确显示所有内容,除了它将文本(前景)显示为黑色而不是我的样式。

解决方案:我可以通过将我的 ItemsPresenter 修改为以下任一项来解决问题(尽管我不明白为什么这是必要的):
<ItemsPresenter TextElement.Foreground="{StaticResource DataGridItemTextBrush}"/>

或者
<ItemsPresenter TextBlock.Foreground="{StaticResource DataGridItemTextBrush}" />

问题:谁能解释为什么会发生这种情况和/或提供更好的解决方案来保证我的 ItemsPresenter 不会覆盖我的任何 DataGrid 样式?

谢谢!

最佳答案

除非 child 选择覆盖强加的样式,否则样式会向下级联。 ItemsPresenter在你的情况下有默认值;您没有在 DataGrid 中覆盖它风格。创建一个 ItemsPresenter风格内App.xaml满足您的需求或通过显式或隐式样式在本地修改值,或选择您建议的解决方案。

另请记住,您可以使用 BasedOn继承默认样式的属性;仅覆盖某些属性。

BasedOn="{StaticResource {x:Type DataGrid}}"

关于wpf - 为什么 ItemsPresenter 会覆盖我的 DataGrid 的前景样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2567822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com