C# 根据不同列的值更改单元格背景

C# 根据不同列的值更改单元格背景,c#,wpf,datagrid,C#,Wpf,Datagrid,我有一个数据网格: 我希望能够根据工作列在每个单元格上着色。 例如,如果单元格的值为1,并且辅助对象为Chef->Color=Blue,否则为透明 如果细胞工作者是服务员,我需要能够改变10至20颜色之间的细胞,如果他们持有任何值或透明,如果没有数据存储 我尝试使用转换器,但问题是它获取单元格的当前数据,我不知道如何获取worker字段值(这是一个组合框) 所以基本上取决于Worker列,如果填充了单元格,我必须应用着色,但我真的在这一点上卡住了 编辑: DataGrid XAML: <

我有一个数据网格:

我希望能够根据工作列在每个单元格上着色。 例如,如果单元格的值为1,并且辅助对象为Chef->Color=Blue,否则为透明

如果细胞工作者是服务员,我需要能够改变10至20颜色之间的细胞,如果他们持有任何值或透明,如果没有数据存储

我尝试使用转换器,但问题是它获取单元格的当前数据,我不知道如何获取worker字段值(这是一个组合框)

所以基本上取决于Worker列,如果填充了单元格,我必须应用着色,但我真的在这一点上卡住了

编辑: DataGrid XAML:

<DataGrid x:Name="dgSchedules"
                    AutoGenerateColumns="False" Background="White"
                    ItemsSource="{Binding ScheduleDetailsViewSource, Mode=TwoWay}"
                    HeadersVisibility="All"
                    CanUserResizeColumns="False"
                    SelectedItem="{Binding DgSelectedScheduleDetail, Mode=TwoWay}"
                    CanUserAddRows="False"
                    AllowDrop="False"
                    CanUserDeleteRows="False" >
                    <DataGrid.Columns>
                        <DataGridTextColumn  IsReadOnly="True"
                            Binding="{Binding Day, StringFormat={}{0:dddd dd.MM.yyyy}}"

                            Width="150"
                            Header="Day" >
                        </DataGridTextColumn>



                        <DataGridTextColumn  Visibility="Collapsed"
                            Binding="{Binding Col1, Mode=TwoWay}"
                            Width="50"
                            Header="Day" >
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Background"
                                         Value="{Binding Col1, Mode=TwoWay}" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col2, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col3, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col4, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col5, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col6, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col7, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col8, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col9, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col10, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col11, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />
                        <DataGridTextColumn IsReadOnly="True"  Visibility="Collapsed"
                            Binding="{Binding Col12, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed" 
                            Binding="{Binding Col13, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col14, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col15, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col16, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col17, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col18, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col19, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col20, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col21, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col22, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col23, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridTextColumn Visibility="Collapsed"
                            Binding="{Binding Col24, Mode=TwoWay}"
                            Width="50"
                            Header="Day" />

                        <DataGridComboBoxColumn
                            Width="100"
                            DisplayMemberPath="Description"
                            SelectedValuePath="Group_Ref"
                            SelectedValueBinding="{Binding Group_Ref}"
                            Header="Worker" >
                            <DataGridComboBoxColumn.ElementStyle>
                                <Style TargetType="{x:Type ComboBox}">
                                    <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ScheduleGroups, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                                </Style>
                            </DataGridComboBoxColumn.ElementStyle>

                            <DataGridComboBoxColumn.EditingElementStyle>
                                <Style TargetType="{x:Type ComboBox}">
                                    <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ScheduleGroups, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                                </Style>
                            </DataGridComboBoxColumn.EditingElementStyle>
                        </DataGridComboBoxColumn>
                    </DataGrid.Columns>
                    <DataGrid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Delete" Click="Context_Delete"/>
                            <MenuItem Header="Insert" Click="Context_Insert"/>
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                    <DataGrid.GroupStyle>
                        <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                            <GroupStyle.Panel>
                                <ItemsPanelTemplate>
                                    <DataGridRowsPresenter/>
                                </ItemsPanelTemplate>
                            </GroupStyle.Panel>
                        </GroupStyle>
                    </DataGrid.GroupStyle>
                </DataGrid>

分组的样式:

 <Style x:Key="GroupHeaderStyle" TargetType ="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <StackPanel>
                        <TextBlock Text="{Binding Day}"/>
                            <ItemsPresenter/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我的模型:

  public partial class Scheduledetail
    {
        public Scheduledetail()
        {
            this.Schedule_Ref = 0;
            this.Group_Ref = 0;
            this.Col1 = "";
            this.Col2 = "";
            this.Col3 = "";
            this.Col4 = "";
            this.Col5 = "";
            this.Col6 = "";
            this.Col7 = "";
            this.Col8 = "";
            this.Col9 = "";
            this.Col10 = "";
            this.Col11 = "";
            this.Col12 = "";
            this.Col13 = "";
            this.Col14 = "";
            this.Col15 = "";
            this.Col16 = "";
            this.Col17 = "";
            this.Col18 = "";
            this.Col19 = "";
            this.Col20 = "";
            this.Col21 = "";
            this.Col22 = "";
            this.Col23 = "";
            this.Col24 = "";
        }

        public int ScheduleDetails_Ref { get; set; }
        public int Schedule_Ref { get; set; }
        public int Group_Ref { get; set; }
        public Nullable<System.DateTime> Day { get; set; }
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public string Col4 { get; set; }
        public string Col5 { get; set; }
        public string Col6 { get; set; }
        public string Col7 { get; set; }
        public string Col8 { get; set; }
        public string Col9 { get; set; }
        public string Col10 { get; set; }
        public string Col11 { get; set; }
        public string Col12 { get; set; }
        public string Col13 { get; set; }
        public string Col14 { get; set; }
        public string Col15 { get; set; }
        public string Col16 { get; set; }
        public string Col17 { get; set; }
        public string Col18 { get; set; }
        public string Col19 { get; set; }
        public string Col20 { get; set; }
        public string Col21 { get; set; }
        public string Col22 { get; set; }
        public string Col23 { get; set; }
        public string Col24 { get; set; }

        public virtual Group Group { get; set; }
        public virtual Schedule Schedule { get; set; }
    }
公共部分类Scheduledetail
{
公共时间表详细信息()
{
本附录附录_Ref=0;
此.Group_Ref=0;
此参数为0.Col1=“”;
此.Col2=“”;
这是1.Col3=“”;
这是1.Col4=“”;
此参数为0.Col5=“”;
这是1.Col6=“”;
此为.Col7=“”;
这是1.Col8=“”;
此为.Col9=“”;
此为.Col10=“”;
此为.Col11=“”;
此为.Col12=“”;
此为.Col13=“”;
此为.Col14=“”;
此为.Col15=“”;
此为.Col16=“”;
此为.Col17=“”;
此为.Col18=“”;
此为.Col19=“”;
此为.Col20=“”;
此为.Col21=“”;
此为.Col22=“”;
此为.Col23=“”;
此为.Col24=“”;
}
public int ScheduleDetails_Ref{get;set;}
public int Schedule_Ref{get;set;}
公共int组_Ref{get;set;}
公共可空日{get;set;}
公共字符串Col1{get;set;}
公共字符串Col2{get;set;}
公共字符串Col3{get;set;}
公共字符串Col4{get;set;}
公共字符串Col5{get;set;}
公共字符串Col6{get;set;}
公共字符串Col7{get;set;}
公共字符串Col8{get;set;}
公共字符串Col9{get;set;}
公共字符串Col10{get;set;}
公共字符串Col11{get;set;}
公共字符串Col12{get;set;}
公共字符串Col13{get;set;}
公共字符串Col14{get;set;}
公共字符串Col15{get;set;}
公共字符串Col16{get;set;}
公共字符串Col17{get;set;}
公共字符串Col18{get;set;}
公共字符串Col19{get;set;}
公共字符串Col20{get;set;}
公共字符串Col21{get;set;}
公共字符串Col22{get;set;}
公共字符串Col23{get;set;}
公共字符串Col24{get;set;}
公共虚拟组{get;set;}
公共虚拟时间表{get;set;}
}
在运行时,我根据表单上以前的用户输入显示所需的网格列:

    if(snd.WorkAliasCollection.View.CurrentItem == null)
        {
            return;
        }
        var entity = snd.WorkAliasCollection.View.CurrentItem as Schedule;
        DateTime? fromDate = entity.FromDate;
        DateTime? toDate = entity.ToDate;
        int startTime = fromDate.Value.Hour;

        int diff = (toDate.Value.Hour - fromDate.Value.Hour);

        dgSchedules.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = snd.ScheduleDetailsViewSource });
        ScheduleDetailsTI.Visibility = Visibility.Visible;

        if (dgSchedules != null)
        {
            for (int i = 0; i <= diff; i++)
            {
                dgSchedules.Columns[i+1].Visibility = System.Windows.Visibility.Visible;
                dgSchedules.Columns[i + 1].Header = startTime++;
            }
        }
if(snd.WorkAliasCollection.View.CurrentItem==null)
{
返回;
}
var entity=snd.WorkAliasCollection.View.CurrentItem作为计划;
约会时间?fromDate=实体。fromDate;
约会时间?toDate=entity.toDate;
int startTime=fromDate.Value.Hour;
int diff=(toDate.Value.Hour-fromDate.Value.Hour);
dgSchedules.SetBinding(ItemsControl.ItemsSourceProperty,新绑定{Source=snd.ScheduleDetailsViewSource});
ScheduleDetailsTI.Visibility=可见性.Visibility;
如果(dgSchedules!=null)
{

对于(int i=0;i为您提供的简单示例:

主窗口代码:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            FillList();
        }

        private void FillList()
        {
            WorkingItems.Add(new WorkingItem {DateTime = DateTime.Today, Worker = "Chef"});
            WorkingItems.Add(new WorkingItem {DateTime = DateTime.Today, Worker = "Waiter"});
            WorkingItems.Add(new WorkingItem {DateTime = DateTime.Today.AddDays(1), Worker = "Chef"});
            WorkingItems.Add(new WorkingItem {DateTime = DateTime.Today.AddDays(2), Worker = "Other"});
            WorkingItems.Add(new WorkingItem {DateTime = DateTime.Today.AddDays(3), Worker = "Nobody"});
        }

        public ObservableCollection<WorkingItem> WorkingItems { get; set; } = new ObservableCollection<WorkingItem>();
    }
我的测试工作项目:

    public class WorkingItem
    {
        public string Worker { get; set; }
        public DateTime DateTime { get; set; }
    }
转换器也可以直接采用其他颜色:

<converter:WorkerToColorConverter x:Key="WorkerToColorConverter" ChefColor="Beige"/>
在XAML中使用:

<Window.Resources>
    <converter:WorkerToColorMultiConverter x:Key="WorkerToColorMultiConverter" />
</Window.Resources>


<converter:WorkerToColorConverter x:Key="WorkerToColorConverter" ChefColor="Beige"/>
    public class WorkerToColorMultiConverter : IMultiValueConverter
    {
        public Brush ChefColor { get; set; }
        public Brush WaiterColor { get; set; }
        public Brush DefaultColor { get; set; }

        public WorkerToColorMultiConverter()
        {
            //Default Colors
            ChefColor = Brushes.Aqua;
            WaiterColor = Brushes.Yellow;
            DefaultColor = Brushes.Transparent;
        }

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values != null && values.Count() == 2)
            {
                var cellValue = values[0] as string;
                var workerValue = values[1] as string;
                if (!string.IsNullOrEmpty(cellValue))
                {
                    switch (workerValue)
                    {
                        case "Chef":
                            return ChefColor;
                        case "Waiter":
                            return WaiterColor;
                        default:
                            return DefaultColor;
                    }
                }
            }
            return DefaultColor;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Window.Resources>
    <converter:WorkerToColorMultiConverter x:Key="WorkerToColorMultiConverter" />
</Window.Resources>
<DataGridTextColumn Header="1" Binding="{Binding Col1}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource WorkerToColorMultiConverter}">
                        <Binding Path="Col1" />
                        <Binding Path="Worker" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="2" Binding="{Binding Col2}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource WorkerToColorMultiConverter}">
                        <Binding Path="Col2" />
                        <Binding Path="Worker" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>