简体   繁体   English

交替网格行颜色?

[英]Alternate Grid row colors?

Is it possible to attach a style or define a property in the WPF System.Windows.Controls.Grid in order to alternate the color of the rows(or columns)? 是否可以在WPF System.Windows.Controls.Grid中附加样式或定义属性以交替行(或列)的颜色?

I need that User be able to place a grid placeholder (something similar to CrystalReports)... It can parameter the grid, but the grid (in View) will not contain any data. 我需要用户能够放置网格占位符(类似于CrystalReports)...它可以参数网格,但网格(在视图中)将不包含任何数据。

The only way I can think of doing this is by hand using a Rectangle either declared before the row contents or with it's Z order set to push it to the back: 我可以想到这样做的唯一方法是手动使用在行内容之前声明的Rectangle或者使用它的Z顺序设置将其推送到后面:

<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>

You might be able to use bindings to pass the row number to a converter to return the correct colour rather than hard coding like I have here. 您可以使用绑定将行号传递给转换器以返回正确的颜色,而不是像我在这里那样进行硬编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM