WPF 数据网格 : How do you set the style of the first row only?

标签 wpf xaml datagrid row selectedindex

我尝试寻找答案,但问题已发布 here尚未得到答复。

我尝试过一些复杂的 XAML,但从未成功。如果选择了第一行,下面的代码会将所有行显示为灰色。无论选择哪个行索引,我都只需将第一行灰显。

<TextBlock.Style>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontStyle" Value="Normal"/>
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Path=SelectedIndex, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}" Value="0"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="Gray"/>
                <Setter Property="FontStyle" Value="Italic"/>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</TextBlock.Style>

有人可以帮忙吗?

谢谢。

最佳答案

您可以使用所有 ItemsControls 上的 AlternationCount 属性(引用 ListBox )。

<DataGrid ItemsSource="{Binding Items}"
          AlternationCount="2147483647"
          ... 
          >
    <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Foreground" Value="Gray"/>
                    <Setter Property="FontStyle" Value="Italic"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.ItemContainerStyle>
    ...
</DataGrid>

编辑

<DataGrid ItemsSource="{Binding Items}"
          AlternationCount="2"
          VirtualizingStackPanel.IsVirtualizing="False">
          ... 
          >
    <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="Gray"/>
                </Trigger>
                <DataTrigger
                    Binding="{Binding RelativeSource={RelativeSource Mode=PreviousData}}"
                    Value="{x:Null}">
                    <Setter Property="Foreground" Value="Gray"/>
                    <Setter Property="FontStyle" Value="Italic"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.ItemContainerStyle>
    ...
</DataGrid>

关于WPF 数据网格 : How do you set the style of the first row only?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41621771/

相关文章:

wpf - 如何将 Tooltip 的 DataTemplate 与其父级绑定(bind)?

wpf - 如何处理 MVVM 中的 Wpf DataGrid CellEditEnding 事件?

c# - ASP.NET 2.0 网格中的分页改进

c# - WFP 数据网格行详细信息垂直滚动条不起作用

c# - 如何将文本 block 与属性绑定(bind)

.net - DataGridRow 上的 WPF DataGrid MouseOver

c# - 如何在组合框中添加一些图像作为下拉菜单?

c# - 具有 Observable 的复杂流程

c# - C# 中的最佳 "not is"语法

silverlight - Windows phone 7 滚动查看器问题