C# WPF 在 CellEditEnding 事件后更改数据网格单元格背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16167755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:39:20  来源:igfitidea点击:

C# WPF Change datagrid cell background after CellEditEnding event

c#wpfdatagridcellbackground-color

提问by Enrico

I have a Datagrid(DataGridMeterValues) in my C# wpf application. Everytime the user exits the editing mode of a cell, the CellEditEndingevent is triggered.

我的 C# wpf 应用程序中有一个Datagrid(DataGridMeterValues)。每次用户退出单元格的编辑模式时,都会触发CellEditEnding事件。

Now I wish to change the background of the cell when the event is triggered.

现在我希望在触发事件时更改单元格的背景。

This is what I got so far:

这是我到目前为止得到的:

private void DataGridMeterValues_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
{

// Code to change background color here

}

I am able to change the background of a full row with following code:

我可以使用以下代码更改整行的背景:

e.Row.Background = Brushes.Yellow;

Now my question is, can I do the same but then only for 1 cell (the selected one) not the entire row

现在我的问题是,我可以做同样的事情,但只针对 1 个单元格(选定的单元格)而不是整行

EDIT:This is the XAML for the datagrid

编辑:这是数据网格的 XAML

<DataGrid Grid.Row="3"  Grid.Column="1" AutoGenerateColumns="False"  Name="DataGridMeterValues" ItemsSource="{Binding Path=MeterValuesList, UpdateSourceTrigger=PropertyChanged}" AlternatingRowBackground="LightGray" BorderBrush="Gray" BorderThickness="1" FrozenColumnCount="0" CanUserResizeColumns="False" CanUserResizeRows="False" Margin="2,0" CanUserSortColumns="False" SelectionMode="Single" CanUserReorderColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" HeadersVisibility="Column" GridLinesVisibility="All" AreRowDetailsFrozen="False" IsEnabled="True" CellEditEnding="DataGridMeterValues_CellEditEnding">
            <DataGrid.Resources>
                <Style x:Key="DataGridBase" TargetType="Control">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <TransformGroup>
                                <RotateTransform Angle="-90" />
                                <ScaleTransform ScaleX="1" ScaleY="-1" />
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
                </Style >
                <Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridBase}"/>
                <Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource DataGridBase}"/>                  
            </DataGrid.Resources>

            <DataGrid.LayoutTransform>
                <TransformGroup>
                    <RotateTransform Angle="90" />
                    <MatrixTransform Matrix="-1, 0, 0, 1, 0, 0" />
                </TransformGroup>
            </DataGrid.LayoutTransform>

            <DataGrid.RowHeaderStyle >
                <Style TargetType="DataGridRowHeader">
                    <Setter Property="Content" Value="X" />
                </Style>
            </DataGrid.RowHeaderStyle>
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Action" >
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                <Button Tag="{Binding}" Content="Activate" Width="50" BorderThickness="0" Margin="2" Background="{Binding Path=Activated}" Click="BtnActivate_Click" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Title, UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn Header="Meter Type" Binding="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged}" />

            </DataGrid.Columns>
        </DataGrid>

回答by Savaratkar

private void DataGridMeterValues_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
{

        FrameworkElement element = e.Column.GetCellContent(DataGridMeterValues.SelectedItem);
        (element.Parent as DataGridCell).Background = new SolidColorBrush(Colors.Red);

}

I have fetched the framework element of the cell in selected row and column. Then I got DataGridCell from that element using its Parent property and set its background property. :)

我已经在选定的行和列中获取了单元格的框架元素。然后我使用其 Parent 属性从该元素获取 DataGridCell 并设置其背景属性。:)

There can be multiple templates inside a DataGridCell. Its better we fetched the DataGridCell and used its background property.

DataGridCell 中可以有多个模板。我们最好获取 DataGridCell 并使用其背景属性。