逐个着色WPF DataGridRows
我正在制作一个WPF程序,它能够使用逐个着色WPF DataGridRows,wpf,database,datagrid,wpfdatagrid,wpftoolkit,Wpf,Database,Datagrid,Wpfdatagrid,Wpftoolkit,我正在制作一个WPF程序,它能够使用for循环将DataGrid中的行逐个涂成红色,我遇到了一些奇怪的事情。如果DataGrid具有来自数据库表的40多行数据,则不会对所有行着色 这是我正在使用的代码 private void Red_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < dataGrid1.Items.Count; i++) { DataGridRow row = (Da
for
循环将DataGrid
中的行逐个涂成红色,我遇到了一些奇怪的事情。如果DataGrid
具有来自数据库表的40多行数据,则不会对所有行着色
这是我正在使用的代码
private void Red_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < dataGrid1.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(i);
if (row != null)
{
row.Background = Brushes.Red;
}
}
}
private void Red\u单击(对象发送者,路由目标)
{
对于(int i=0;i
是否有其他方法可以通过其他方法逐个为行着色,或者这是wpftoolkit中的某种故障?屏幕上不可见的行将无法使用此方法着色,因为它们被虚拟化,并且实际上不存在。在下面的样式中,im绑定到一个属性,以使行在红色和默认颜色之间切换(将其放入from的资源中,并在其上添加数据网格) 然后在我的xaml中,我在顶部有声明
<Window
x:Class="Grids.Window1"
x:Name="self">
如果要为栅格的所有行设置背景,可以定义新的行样式对象并设置其背景属性;这应该一次更改所有背景行,而不需要遍历它们。Smth是这样的:
dataGrid1.RowStyle = new Style(typeof(DataGridRow));
dataGrid1.RowStyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Red)));
<Window.Resources>
<Style x:Key="customDataGridRowStyle" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Test1}" Value="1">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
..
<DataGrid .. DataGrid.RowStyle ="{StaticResource customDataGridRowStyle}" >
..
您还可能需要根据数据网格行后面的数据对象的状态更改数据网格行的背景。在这种情况下,可以在xaml中设置带有触发器的自定义样式,并将其指定为行样式。我想smth是这样的:
dataGrid1.RowStyle = new Style(typeof(DataGridRow));
dataGrid1.RowStyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Red)));
<Window.Resources>
<Style x:Key="customDataGridRowStyle" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Test1}" Value="1">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
..
<DataGrid .. DataGrid.RowStyle ="{StaticResource customDataGridRowStyle}" >
..
..
..
在上面的示例中,只要“Test1”属性的值为“1”,就会将红色背景设置为行
希望这能有所帮助,请注意如果要为每行定义颜色,并且在行显示的项目上有一个属性,则可以使用ItemsContainerStyle设置行颜色。在下面的示例中,网格中的项目上有一个名为ItemColor的属性,该属性将定义背景行颜色。绑定从该行绑定到该行包含的项
<dg:DataGrid.ItemContainerStyle>
<Style
TargetType="{x:Type dg:DataGridRow}"
BasedOn="{StaticResource {x:Type dg:DataGridRow}}">
<Setter
Property="Background"
Value="{Binding ItemColour}" />
</Style>
</dg:DataGrid.ItemContainerStyle>
但您可能不希望在项目上使用属性ItemColor,因为它们可能是您的业务模式。这就是ViewModel自身的优势所在。您可以定义一个中间层,该层基于一些自定义逻辑包装业务层和ItemColor属性 我明白了。如果行不存在,因为它们在数据网格上看不到,这是否意味着不可能使用多种颜色(例如:白色/黑色/灰色或黄色/红色/蓝色)对行进行着色?谢谢你的回答。顺便说一句,我仍然觉得有其他方法可以解决这个问题。你能提供一个关于这个ViewModel的例子吗?在绑定属性方面,我仍然做得不好。下载彩色行示例,并将此问题标记为已回答。可能还有一些其他的项目,你可能也想看看+1,因为它的拼写是用“u”着色的,哦,谢谢你的例子。
<dg:DataGrid.ItemContainerStyle>
<Style
TargetType="{x:Type dg:DataGridRow}"
BasedOn="{StaticResource {x:Type dg:DataGridRow}}">
<Setter
Property="Background"
Value="{Binding ItemColour}" />
</Style>
</dg:DataGrid.ItemContainerStyle>