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

c# – 在WPF中更改网格行背景颜色,第1张

概述我想为我的网格行设置2种颜色,偶数颜色将有一种颜色,其他颜色将有另一种颜色. 我甚至不知道开始做这件事.                                                              <Grid x:Name="Stations_Template"> <Grid.RowDefinitions> 我想为我的网格行设置2种颜色,偶数颜色将有一种颜色,其他颜色将有另一种颜色.
我甚至不知道开始做这件事.


<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,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>
总结

以上是内存溢出为你收集整理的c# – 在WPF中更改网格行背景颜色全部内容,希望文章能够帮你解决c# – 在WPF中更改网格行背景颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1263145.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存