在WPF中更改网格行背景颜色

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在WPF中更改网格行背景颜色相关的知识,希望对你有一定的参考价值。

我想为我的网格行设置2种颜色,偶数颜色将有一种颜色,其他颜色将有另一种颜色。我甚至不知道开始做这件事。

<ListBox
    ItemsSource="{Binding}" x:Name="station_list"
    HorizontalAlignment="Left" Height="378" Margin="10,31,0,0"
    VerticalAlignment="Top" Width="570" SelectedIndex="0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid x:Name="Stations_Template">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="First Name: " />
                <TextBlock Grid.Column="1" Text="{Binding Path=sname}" />
                <TextBlock Grid.Column="2" Text="Last Name: " />
                <TextBlock Grid.Column="3" Text="{Binding Path=mahoz}" />
                <CheckBox Grid.Column="4" Content="Is Active?"
                    IsEnabled="False"
                    IsChecked="{Binding Path=isactive}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
答案

使用矩形首先填充行,然后向它们添加数据。

    <Grid Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Rectangle Grid.Row="0" Fill="AliceBlue" />
        <TextBlock Grid.Row="0" Text="Row 1" HorizontalAlignment="Center"/>
        <Rectangle Grid.Row="1" Fill="AntiqueWhite" />
        <TextBlock Grid.Row="1" Text="Row 2" HorizontalAlignment="Center"/>
        <Rectangle Grid.Row="2" Fill="AliceBlue" />
        <TextBlock Grid.Row="2" Text="Row 3" HorizontalAlignment="Center"/>
        <Rectangle Grid.Row="3" Fill="AntiqueWhite" />
        <TextBlock Grid.Row="3" Text="Row 4" HorizontalAlignment="Center"/>
    </Grid>

编辑:如果您正在加载未知数量的项目,那么我认为您需要像itemscontrol这样的东西加载它们。然后您可以使用alternationcount和触发器来处理交替颜色。

    <ItemsControl ItemsSource="{Binding DataList}" AlternationCount="2">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="FooBar" Margin="0,0,0,10">

                </Grid>
                <DataTemplate.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="Blue" TargetName="FooBar"/>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="Red" TargetName="FooBar"/>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
另一答案

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Border Background="Cyan" />
        <Border Grid.Row="2" Grid.Column="1"  Background="Red" />
        <Border  Grid.Row="1" Background="Black" />
        <Border  Grid.Row="1" Background="Black" />
        <Border Grid.Row="2" Background="Orange" />
        <Border Grid.Row="0" Grid.Column="1" Background="Green" />
        <TextBlock Grid.ColumnSpan="2" Grid.Row="1" Text="Here is some more text" HorizontalAlignment="Center"  VerticalAlignment="Center"/>
        <TextBlock Grid.ColumnSpan="2" Text="Here is some text" HorizontalAlignment="Center"  VerticalAlignment="Center"/>
        <TextBlock Grid.ColumnSpan="2" Grid.Row="2" Text="Here is even more text" HorizontalAlignment="Center"  VerticalAlignment="Center"/>
    </Grid>
</Window>
![enter image description here][1]


  [1]: http://i.stack.imgur.com/LX9X8.png

以上是关于在WPF中更改网格行背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式更改 WPF 中的数据网格行颜色?

选择后更改整个数据网格行的背景颜色

如何使用从wpf中的数据库中获取的值更改datagrid行背景颜色

WPF使用转换器更改datagrid单元格背景颜色

C#WPF更改网格背景图像[重复]

使用 WPF 数据网格时如何更改列标题的背景颜色