【问题标题】:Why does ItemsPresenter override my DataGrid's foreground style?为什么 ItemsPresenter 会覆盖我的 DataGrid 的前景样式?
【发布时间】:2010-04-02 16:34:17
【问题描述】:

我在 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 样式?

谢谢!

【问题讨论】:

    标签: wpf datagrid styles itemspresenter


    【解决方案1】:

    除非孩子选择覆盖强加的样式,否则样式会向下级联。在您的情况下,ItemsPresenter 具有默认值;您不会在 DataGrid 样式中覆盖它。要么在 App.xaml 中创建 ItemsPresenter 样式以满足您的需求,要么通过显式或隐式样式在本地修改值,或者选择您建议的解决方案。

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

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

    【讨论】:

    • 唉...一个答案!迟到总比不到好,对吧?有几件事...... 1)为什么 ItemsPresenter 只更改我的 DataGrid 的“前景”,而不是它的“背景”(例如,如果我将 DataGrid 的样式的背景更改为绿色而不是透明) 2)设置样式ItemsPresenter 要基于 DataGrid 的样式会引发异常。
    • 我确实有一些简单的解决方法来解决我的问题......但我最关心的是“为什么 ItemsPresenter 会更改我的 DataGrid 的前景,但它不会更改 DataGrid 的其他属性,例如背景。” ItemsPresenter 甚至没有'Foreground' 属性......所以它不像'Foreground' 有自己的默认值。如果确实如此……它将继承自 DataGrid 的样式。
    • @Scottss DataGrid BasedOn 参考是为了继承 DataGrid 的标准外观,而不是从头开始。 ItemsPresenter 没有 Foreground 属性,但它必须呈现它正在呈现的文本,并且 ControlTemplate 中的控件很可能存在执行此操作,因此您可以通过 TextBlock 类型更改 Foreground。在本地 XAML 文件中为 TextBlock 创建默认样式,将前景设为红色,看看会发生什么。
    • 所以我玩弄了一些东西,最后发现不是 ItemsPresenter 破坏了我的样式继承。如果我将 Expander 的前景设置为红色,则一切都会正确继承它。如果我在样式设置器中将 GroupItem 的前景设置为红色,则不会继承它。如果我将 GroupItem 的样式设置为基于我的 DataGrid 的样式...一切都会正确继承。我仍然不明白为什么......但我显然在一年前专注于错误的问题。我感觉它与 GroupItem 的 ControlTemplate 或 GroupItem 样式本身有关。
    • 我仍然给了你一个赞成票和建议使用 BasedOn 的答案,因为如果我将它与我的 GroupItem 样式一起使用,它就可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2021-06-07
    • 2016-04-08
    • 1970-01-01
    • 2015-03-24
    • 2013-01-31
    • 1970-01-01
    相关资源
    最近更新 更多