如何更改/设置数据网格中单行的背景颜色?

时间:2015-02-02 11:31:07

标签: c# wpf xaml datagrid

如何使用c#?

更改数据网格中单行的背景颜色

2 个答案:

答案 0 :(得分:2)

试试这段代码:

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());
            }
        }
    }

答案 1 :(得分:0)

尝试将item-template的Background属性绑定到data-item的属性。上面的代码用于List-item模板: 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>