question

LeClairMichaelLRumford-2175 avatar image
0 Votes"
LeClairMichaelLRumford-2175 asked ·

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

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-wpfdotnet-visual-basicdotnet-wpf-xaml
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-MSFT avatar image
0 Votes"
DaisyTian-MSFT answered ·

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.



capture.png (15.6 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.