【问题标题】:How do I overwrite AlternatingRowBackground inside the DataGridRow template?如何覆盖 DataGridRow 模板中的 AlternatingRowBackground?
【发布时间】:2012-08-18 21:18:18
【问题描述】:

我正在创建一个带有 AlternatingRowBackground 属性的行的 DataGrid。但是,必须修改行中的数据,这需要一些时间。

我正在尝试使行的背景颜色在初始化时显示为浅灰色。这是我在 RowTemplate 中所做的:

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding Initialized}" Value="False">
        <Setter Property="Background" Value="LightGray"/>
    </DataTrigger>
</ControlTemplate.Triggers>

但这不适用于仍然具有 AlternatingRowBackground 中指定颜色的奇数行。

如何覆盖它,使所有未初始化的行都显示为浅灰色?

【问题讨论】:

  • 您是否也尝试将此 DataTrigger 放入交替行的模板中?我的猜测是备用行的模板是在带有数据触发器的模板之后应用的。
  • 不,我没有那样做。我该怎么做?
  • 对不起,我说错了。看看这个答案:stackoverflow.com/a/3614918/563088 您可以尝试将 datatrigger 与用于交替行背景的设置器放在一个样式中。

标签: c# wpf xaml datagrid


【解决方案1】:

我遇到了同样的问题,接下来为我工作: 在样式中设置 AlternatingRowBackground,而不是在 DataGrid 中。

    <Grid.Resources>
        <Style x:Key="dg" TargetType="DataGrid">
            <Setter Property="AlternatingRowBackground" Value="Orange"/>
            <Setter Property="AutoGenerateColumns" Value="False"/>
        </Style>
    </Grid.Resources>
    <DataGrid ItemsSource="{Binding Source={StaticResource one}, Path=Persons}" Style="{StaticResource dg}">
        <DataGrid.Columns>
            <DataGridTextColumn Width="*" Header="Name" Binding="{Binding Path=Name}"/>
        </DataGrid.Columns>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Mature}" Value="True">
                        <Setter Property="Background" Value="LightGray"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2011-07-08
    • 2014-05-22
    • 2017-09-03
    • 1970-01-01
    • 2013-09-18
    • 2018-06-02
    相关资源
    最近更新 更多