如何 change/set 数据网格中单行的背景色?
How to change/set background color of a single row in a datagrid?
如何使用 C# 更改数据网格中单行的背景颜色?
尝试将项目模板的背景 属性 绑定到数据项目的 属性。上面的代码用于列表项模板:
XAML
<DataTemplate>
<StackPanel x:Name="itemBlock">
...
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsIncluded}" Value="False">
<Setter Property="Background" TargetName="itemBlock" Value="#DDDCAB"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsIncluded}" Value="True">
<Setter Property="Background" TargetName="itemBlock" Value="Transparent"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
试试这个代码:
XAML:
<Window.Resources>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Property }" Value="True">
<Setter Property="Background" Value="#FAFAFA" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
C#:
private void MarkRowAsDone(object sender, MouseButtonEventArgs e)
{
using (var context = new Context())
{
try
{
//If you have set a ID, get that ID to do a "select"-statement
var row = context.CLASS.Find(selectedItem.Id);
row.Done = true;
context.SaveChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
如何使用 C# 更改数据网格中单行的背景颜色?
尝试将项目模板的背景 属性 绑定到数据项目的 属性。上面的代码用于列表项模板:
XAML
<DataTemplate>
<StackPanel x:Name="itemBlock">
...
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsIncluded}" Value="False">
<Setter Property="Background" TargetName="itemBlock" Value="#DDDCAB"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsIncluded}" Value="True">
<Setter Property="Background" TargetName="itemBlock" Value="Transparent"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
试试这个代码:
XAML:
<Window.Resources>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Property }" Value="True">
<Setter Property="Background" Value="#FAFAFA" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
C#:
private void MarkRowAsDone(object sender, MouseButtonEventArgs e)
{
using (var context = new Context())
{
try
{
//If you have set a ID, get that ID to do a "select"-statement
var row = context.CLASS.Find(selectedItem.Id);
row.Done = true;
context.SaveChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}