How to Change Cell background color in WPF Datagrid

为君一笑 提交于 2020-06-27 04:10:22

问题


I want to change the cell background color in my WPF Datagrid application when I select a cell and click on "Red Button" or "Blue Button" in UI.

I'm a beginner to this WPF Datagrid architecture and really appreciate if someone can help me with this. (I'm using MVVM for my application)


回答1:


Something like this:

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



回答2:


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

And the sample 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>



回答3:


Try this code:

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

Output: Screenshot



来源:https://stackoverflow.com/questions/29001803/how-to-change-cell-background-color-in-wpf-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!