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

Time:02-15

I want to change the color of a cell in a data grid if the value entered is out of range.Column to be edited is "Value1". How best to do it? Thanks in advance. 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.
            
        }

CodePudding user response:

You could set the Background of the DataGridCell directly:

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

This works as long as the cells aren't virtualized.

A more robust solution would be to use a CellStyle that binds to a property that you set when the value overflows:

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

The property should be added to the class where the Value1 property is defined and set in the event handler:

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

Make sure that the class implements the INotifyPropertyChanged interface and raise the PropertyChanged event for the Overflows property.

  • Related