c# – 在WPF中更改网格行背景颜色
发布时间:2020-12-15 04:24:08 所属栏目:百科 来源:网络整理
导读:我想为我的网格行设置2种颜色,偶数颜色将有一种颜色,其他颜色将有另一种颜色. 我甚至不知道开始做这件事. Grid x:Name="Stations_Template" Grid.RowDefinitions RowDefinition Height="Auto" / /Grid.RowDefinitions Grid.ColumnDefinitions ColumnDefiniti
我想为我的网格行设置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 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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |