VB.NET WPF Datagrid Cell Background color based on value.

LeClair, Michael L. (Rumford) 21 Reputation points
2021-03-08T14:31:56.483+00:00

VB.NET WPF Datagrid Cell Background color based on value.

I need help with conditional formatting of individual cells within a datagrid based on the value of that cell. I can't seem to find the right answer elsewhere. Can someone provide examples?

Mike

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,674 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,575 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2021-03-09T06:16:47.557+00:00

    I will show you a demo to implement DatagridCell Background color based on value.
    The Xaml code is:

      <DataGrid x:Name="dataGrid" ItemsSource="{Binding }" AutoGenerateColumns="False"  SelectionMode="Extended" SelectionUnit="CellOrRowHeader" >  
                <DataGrid.Resources>  
                    <local:WorkItemBackgroundConverter x:Key="converter"/>  
                </DataGrid.Resources>  
                <DataGrid.Columns>  
                    <DataGridTemplateColumn Header="Name " Width="50" CanUserSort="True" x:Name="SP001" >  
                        <DataGridTemplateColumn.CellTemplate>  
                            <DataTemplate>  
                                <Label x:Name="SP001F" Width="50" Content="{Binding Name}"  HorizontalAlignment="Center" Background="{Binding Path=Name, Converter={StaticResource converter}}"  
                                       HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" VerticalAlignment="Center"  />  
                            </DataTemplate>  
                        </DataGridTemplateColumn.CellTemplate>  
                    </DataGridTemplateColumn>  
                    <DataGridTextColumn Header="Age" Width="50" Binding="{Binding Age}" />  
                    <DataGridCheckBoxColumn Header="Pass Exam?" Width="100"  Binding="{Binding Pass}"/>  
                    <DataGridHyperlinkColumn Header="Email" Width="150"  Binding="{Binding Email}"/>  
                </DataGrid.Columns>  
            </DataGrid>  
    

    The vb code is:
    Imports System.Collections.ObjectModel

    Partial Public Class MainWindow  
        Inherits Window  
        Public Sub New()  
            InitializeComponent()  
            Dim memberData As ObservableCollection(Of Member) = New ObservableCollection(Of Member)()  
            Dim radom As Random = New Random()  
      
            For i As Integer = 1 To 20 - 1  
                Dim men As Member = New Member()  
                men.Age = radom.[Next](100).ToString()  
                men.Name = "JOE" & i.ToString()  
      
                If i Mod 2 = 0 Then  
                    men.Pass = True  
                Else  
                    men.Pass = False  
                End If  
      
                men.Email = New Uri("mailto:JOE" & i.ToString() & "+@school.com")  
                memberData.Add(men)  
            Next  
      
            dataGrid.DataContext = memberData  
        End Sub  
    End Class  
      
      
    Public Class Member  
        Public Property Name As String  
        Public Property Age As String  
        Public Property Pass As Boolean  
        Public Property Email As Uri  
    End Class  
    

    The converter code is:

    Imports System.Globalization  
      
    Class WorkItemBackgroundConverter  
        Implements IValueConverter  
      
        Private Function IValueConverter_Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert  
            If value.ToString().Contains("1") Then  
                Return Brushes.Bisque  
            Else  
                Return Brushes.LightBlue  
            End If  
        End Function  
      
        Private Function IValueConverter_ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack  
            Throw New NotImplementedException()  
        End Function  
    End Class  
    

    The result picture is:
    75704-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments