如果单元格包含特定值,WPF DataGrid将更改单元格背景

时间:2015-12-18 05:18:01

标签: c# wpf xaml datagrid

考虑以下XML代码。我真正想要做的是,如果我的数据网格的单元格包含特定值,例如,如果我的某些数据网格单元格包含值" 1"无论细胞位置在哪里,我希望这些细胞都是浅绿色的。目前我正在尝试使用xml中的触发器,但我被卡住请帮助..

<Window x:Class="WPFDatagridCustomization.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loc="clr-namespace:WPFDatagridCustomization"
    xmlns:local="clr-namespace:WPFDatagridCustomization.HelperClasses"
    Title="MainWindow"
    Width="600"
    Height="500">

<Grid>
    <DataGrid Name="dataGrid1"
              Grid.Row="3"
              Grid.Column="1"
              Margin="10"
              HorizontalAlignment="Left"
              VerticalAlignment="Top"
              AlternationCount="2"
              AutoGenerateColumns="False"
              ItemsSource="{Binding TemperatureCollection}"
              Loaded="dataGrid1_Loaded"
              MinRowHeight="26"

              RowDetailsVisibilityChanged="dataGrid1_RowDetailsVisibilityChanged">

        <DataGrid.Columns>



            <DataGridTextColumn Width="120"
                                Binding="{Binding TableNo}"
                                Header="Table No" />
            <DataGridTextColumn Width="120"
                                Binding="{Binding TableArea}"
                                Header="Unknown Column" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time6}"
                                Header="6:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time630}"
                                Header="6:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time7}"
                                Header="7:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time730}"
                                Header="7:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time8}"
                                Header="8:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time830}"
                                Header="8:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time9}"
                                Header="9:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time930}"
                                Header="9:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time10}"
                                Header="10:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1030}"
                                Header="10:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time11}"
                                Header="11:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1130}"
                                Header="11:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time12}"
                                Header="12:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1230}"
                                Header="12:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time13}"
                                Header="13:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1330}"
                                Header="13:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time14}"
                                Header="14:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1430}"
                                Header="14:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time15}"
                                Header="15:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1530}"
                                Header="15:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time16}"
                                Header="16:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1630}"
                                Header="16:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time17}"
                                Header="17:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1730}"
                                Header="17:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time18}"
                                Header="18:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1830}"
                                Header="18:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time19}"
                                Header="19:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1930}"
                                Header="19:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time20}"
                                Header="20:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time2030}"
                                Header="20:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time21}"
                                Header="21:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time2130}"
                                Header="21:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time22}"
                                Header="22:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time2230}"
                                Header="22:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time23}"
                                Header="23:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time2330}"
                                Header="23:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time24}"
                                Header="24:00" />



        <DataGridTextColumn>
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridTextColumn">
                        <Style.Triggers>
                            <Trigger Property="PorpertyToGetCellContent" Value="1">
                                <Setter Property="Foreground" Value="LightGreen"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

        </DataGrid.Columns>



    </DataGrid>
</Grid>

2 个答案:

答案 0 :(得分:1)

此方法允许根据一个单元格更改颜色,这可能是在特定列中执行此操作的最简单方法。请参阅以下示例:

型号:

@classmethod
def get_create_url(cls):
    return reverse_lazy('school:new-school')

<强> XAML:

public class Person
{
    public int IdPerson { get; set; }
    public string Name { get; set; }
    public string SurName { get; set; }
}

结果: enter image description here

答案 1 :(得分:1)

以下是示例代码

 <Window.Resources>
    <Style TargetType="{x:Type DataGridCell}" x:Key="MyCellStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Id}" Value="2">
                <Setter Property="Background" Value="LightCoral"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid Margin="10">  
    <DataGrid Name="dgUsers" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Id}" CellStyle="{StaticResource ResourceKey=MyCellStyle }" />
            <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
            <DataGridTextColumn Header="Birthday" Binding="{Binding Birthday}" />
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Details}" Margin="10" />
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>

    </DataGrid>
</Grid>

public class User
{
    public int Id { get; set; }

    public string Name { get; set; }

    public DateTime Birthday { get; set; }


}

    public MainWindow()
    {
        InitializeComponent();
        List<User> users = new List<User>();
        users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23) });
        users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17) });
        users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2) });

        dgUsers.ItemsSource = users;
    }

此处单元格的值绑定到Id

Users字段