简体   繁体   English

在 WPF DataGrid 中更改单元格的背景颜色

[英]Change Background color of cell in WPF DataGrid

I need a simple code snippet to change the background color of WPF DataGrid cell .我需要一个简单的code snippet来更改WPF DataGrid cell的背景颜色。 I have column index and row index .我有column indexrow index I want to change color inside CellEditEnding event hadler .我想在CellEditEnding event hadler内更改颜色。 I wrote event handler .我写了event handler Now I need the simple code snippet to change the background color of cell .现在我需要简单的code snippet来更改cellbackground color

Something like the following would allow you to change a specific cell background without having to use selected...类似下面的内容将允许您更改特定的单元格背景而无需使用选定的...

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace SelectDataGridCell
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            DataGridCell cell = GetCell(1, 1, myDataGrid);
                    cell.Background = new SolidColorBrush(Colors.Red);
        }

        public DataGridCell GetCell(int rowIndex, int columnIndex, DataGrid dg)
        {
            DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
            DataGridCellsPresenter p = GetVisualChild<DataGridCellsPresenter>(row);
            DataGridCell cell = p.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
            return cell;
        }

        static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        } 
    }
}
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Class="SelectDataGridCell.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
        <DataGrid x:Name="myDataGrid" Margin="0,0,244,205" AutoGenerateColumns="False" ItemsSource="{Binding Collection}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Property1}" Header="Property1"/>
                <DataGridCheckBoxColumn Binding="{Binding Property2}" Header="Property2"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="75" Margin="8,0,0,146.04" Click="Button_Click"/>
    </Grid>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM