c# wpf 如何在值超出范围时更改数据网格中单元格的颜色

c# wpf how to change color of a cell in a data grid if its value is out of range

如果要编辑的 range.Column 中输入的值是“Value1”,我想更改数据网格中单元格的颜色。怎么做最好? 提前致谢。 xaml:

<TreeViewItem TreeViewItem.Expanded="TreeViewItem1_Expanded" TreeViewItem.Collapsed=" TreeViewItem1_Collapsed" x:Name="Parametertreeviewitem1" Header="Motor Data" VerticalContentAlignment="Center"   FontFamily="Segoe UI" RenderTransformOrigin="0.5,0.5"  FontSize="16" Width="896" HorizontalAlignment="Left" HorizontalContentAlignment="Center" >
                        <DataGrid x:Name="Data_grid1" Height="520" Margin="0,5,0,-25" VerticalAlignment="Top" Width="1150" AutoGenerateColumns="False" SelectionMode="Single" CellEditEnding="CellEditEnding">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Display"  Width="100" Binding="{Binding Display1}" IsReadOnly="True"/>
                                <DataGridTextColumn Header="Parameter" Width="250" Binding="{Binding Parameter1}" IsReadOnly="True"/>
                                <DataGridTextColumn Binding="{Binding Range1}" Header="Range" Width="200" IsReadOnly="True"/>
                                <DataGridTextColumn Binding="{Binding Value1, ValidatesOnExceptions=True}" Header="Drive value" Width="150"/>
                                <DataGridTextColumn Binding="{Binding File_Value1}" Header="File value" Width="150" IsReadOnly="True"/>
                            </DataGrid.Columns>
                        </DataGrid>
                    </TreeViewItem>

cs:

 private void CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        para_num = (byte)Data_grid.SelectedIndex;
        var editedTex = e.EditingElement as TextBox;
        if ((Current_grp[para_num].Min_value <= (Convert.ToDecimal(editedTex.Text)) * Current_grp[para_num].Scale) && (Convert.ToDecimal(editedTex.Text) * Current_grp[para_num].Scale <= Current_grp[para_num].Max_value))
        {
         // This code runs if edit is valid.
         // above condition checks whether parameter is in proper range.
            
        }

你可以直接设置DataGridCellBackground:

private void CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    var editedTex = e.EditingElement as TextBox;
    ...
    DataGridCell cell = FindParent<DataGridCell>(editedTex);
    cell.Background = Brushes.Silver;
}

private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);
    if (parent == null)
        return null;

    return parent as T ?? FindParent<T>(parent);
}

只要单元没有被虚拟化,这就有效。

一个更可靠的解决方案是使用 CellStyle 绑定到您在值溢出时设置的 属性:

<DataGridTextColumn Binding="{Binding Value1, ValidatesOnExceptions=True}" Header="Drive value" Width="150">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Overflows}" Value="True">
                    <Setter Property="Background" Value="Silver" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

应将 属性 添加到 class,其中 Value1 属性 已在事件处理程序中定义和设置:

private void CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    var editedTex = e.EditingElement as TextBox;
    var dataObj = editedTex.DataContext as YourClass;
    ...
    dataObj.Overflows = true;
}

确保 class 实现 INotifyPropertyChanged 接口并为 Overflows 属性.

引发 PropertyChanged 事件