如何在WPF Datagrid中更改单元格背景颜色

时间:2015-03-12 04:19:24

标签: c# wpf mvvm cell wpfdatagrid

当我选择一个单元格并在UI中单击“红色按钮”或“蓝色按钮”时,我想更改WPF Datagrid应用程序中的单元格背景颜色。

我是这个WPF Datagrid架构的初学者,非常感谢有人可以帮助我。 (我在我的应用程序中使用MVVM)

3 个答案:

答案 0 :(得分:0)

这样的事情:

private void Form1_Click(object sender, System.EventArgs e)
{
    this.BackColor = System.Drawing.Color.DarkBlue;
}

答案 1 :(得分:0)

以下内容可让您更改特定的单元格背景,而无需使用选定的...

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

样本xaml ......

    <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>

答案 2 :(得分:0)

尝试以下代码:

<DataGrid Name="SampleDataGrid" 
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Status" Binding="{Binding State}" Width="100">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Background" Value="OrangeRed"/>
                    <Style.Triggers>
                        <Trigger Property="Text" Value="Completed">
                            <Setter Property="Background" Value="GreenYellow"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

输出: Screenshot