如何在WPF DataGrid中为特定单元格分配颜色? [英] How to assign a color to particular cell in wpf datagrid?

查看:63
本文介绍了如何在WPF DataGrid中为特定单元格分配颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是萨拉斯·钱德拉……

Hi this is sarath chandra......


推荐答案

你好,萨拉斯,

.能否请您进一步介绍影响细胞颜色的条件? br/>

基本上,我们可以将CellTemplate中的属性绑定到数据源(例如Background属性显示背景色)

Hello Sarath,

  Could you please tell more about the conditions which affect the cell color?


  Basically we can bind properties in the CellTemplate to data source (For example the Background property shows the back color)

  <DataTemplate x:Key="DateTemplate" >
            
                <Border Background="{Binding CellColorData}" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate}" FontSize="9" HorizontalAlignment="Center" />
                </Border>
      
  </DataTemplate>

在这里,我们将Background绑定到数据源的CellColorData属性.

完成示例:

 Here we bind the Background to the CellColorData property of the Data Source.

  Complete sample:

 

<Window x:Class="CellColorDataGrid.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CellColorDataGrid"
    xmlns:core="clr-namespace:System;assembly=mscorlib"
        
    Title="Window1" Height="600" Width="800" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
    
    <Window.Resources>

        <DataTemplate x:Key="DateTemplate" >
            
                <Border Background="{Binding CellColorData}" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate}" FontSize="9" HorizontalAlignment="Center" />
                </Border>
      
        </DataTemplate>

        

        <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type core:Enum}" x:Key="myEnum">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:MusicCategory" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    
    <Grid>
       
        
        
        <my:DataGrid Name="instdataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False">
            <my:DataGrid.Columns>
                
                <my:DataGridTextColumn Header="Title" Binding="{Binding Path=Title}"/>
                <my:DataGridCheckBoxColumn Header="In Collection?" Binding="{Binding InCollection}" />
                <my:DataGridTemplateColumn Header="Date Published" CellTemplate="{StaticResource DateTemplate}" />
                <my:DataGridComboBoxColumn Header="Category" SelectedItemBinding="{Binding Category}" ItemsSource="{Binding Source={StaticResource myEnum}}" />
                <my:DataGridHyperlinkColumn Header="Artist Website" Binding="{Binding Path=ArtistWebsite}" />
          
            </my:DataGrid.Columns>
        </my:DataGrid>
        
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CellColorDataGrid
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            // Set the DataGrid Context.

            instdataGrid.DataContext = new MusicData();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Media;

namespace CellColorDataGrid
{
    public class MusicData : ObservableCollection<Music>
    {

        public MusicData()
        {
            Add(new Music("Chris Sells Live", true, new DateTime(1999, 3, 23), MusicCategory.Country, new Uri("http://www.msn.com")) { CellColorData = Brushes.Beige });
            Add(new Music("Without My IDE", true, new DateTime(2005, 6, 8), MusicCategory.Pop, new Uri("http://www.yahoo.com")) { CellColorData = Brushes.Brown });
            Add(new Music("If You Only Knew C", false, new DateTime(1991, 2, 24), MusicCategory.Pop, new Uri("http://www.msn.com")) { CellColorData = Brushes.DarkBlue });
            Add(new Music("The Road to Redmond", true, new DateTime(1977, 5, 1), MusicCategory.Country, new Uri("http://www.msn.com")) { CellColorData = Brushes.GreenYellow });
            Add(new Music("Coding Out of a Box", false, new DateTime(2007, 4, 2), MusicCategory.Pop, new Uri("http://www.msn.com")) { CellColorData = Brushes.MediumVioletRed });
            Add(new Music("The Best of Jim Hance", true, new DateTime(1988, 10, 9), MusicCategory.Classical, new Uri("http://www.msn.com")) { CellColorData = Brushes.Purple });
        }
    }

    public class Music
    {
        public Music()
        {
        }

        public Music(string title, bool incollection, DateTime publishdate, MusicCategory category, Uri artistwebsite)
        {
            this.Title = title;
            this.InCollection = incollection;
            this.PublishDate = publishdate;
            this.Category = category;
            this.ArtistWebsite = artistwebsite;
        }

        public string Title { get; set; }
        public bool InCollection { get; set; }
        public DateTime PublishDate { get; set; }
        public Uri ArtistWebsite { get; set; }
        public MusicCategory Category { get; set; }

        public List<string> ItemsPropertyForCombobox
        {
            get;
            set;
        }

        public Brush CellColorData
        {
            get;
            set;
        }

    }

    //Enum for musical category
    public enum MusicCategory
    {
        Pop,
        Classical,
        Country
    };
}




如有任何问题,请告诉我.

谢谢.


  

  If you have any question please let me know.

  Thanks.


这篇关于如何在WPF DataGrid中为特定单元格分配颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆