while DataGridCell.IsEditing 为真时如何更改 DataGridRow 颜色?

How to change DataGridRow Color when while DataGridCell.IsEditing is true?

当 DataGridCell.IsEditing 为真时如何更改 DataGridRow 颜色?

当用户双击单元格时,它会突出显示文本并取消选择整行(即将行颜色更改为透明)。应该只突出显示当时选择的内容,当用户在编辑或离开字段后失去焦点时,该行可以再次突出显示,具体取决于光标所在的行。

    <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Style.Triggers>
                        <Trigger Property="DataGridCell.IsEditing" Value="True">
                            <Setter Property="BorderThickness" Value="3" />                          
                            <Setter Property="BorderBrush" Value="DarkBlue" />    

//Change DataGridRow BackGroudCorlor to transparent.                           
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>

您可以使用数据网格的 BeginningEdit 事件和 CellEditEnding 事件实现相同的效果。参考下面的代码。

 <StackPanel>            
        <DataGrid BeginningEdit="DataGrid_BeginningEdit" CellEditEnding="DataGrid_CellEditEnding" x:Name="dgr"></DataGrid>
    </StackPanel>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ObservableCollection<person> lst = new ObservableCollection<person>();
        for (int i = 0; i < 10; i++)
        {
            lst.Add(new person() { FirstName = "Test" + i, LastName = "Lst" + i });

        }
        dgr.ItemsSource = lst;

    }

    private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
       e.Row.Resources.Add(SystemColors.HighlightBrushKey, Brushes.Transparent);
    }

    private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        e.Row.Resources.Remove(SystemColors.HighlightBrushKey);            
    }
}