-
Oct 23rd, 2016, 02:14 PM
#1
Thread Starter
New Member
Change datagrid cell color based on the value of the cell?
I've this DataGrid structure:
Code:
<DataGrid ItemsSource="{Binding MatchService.Matches}" AutoGenerateColumns="False"
CanUserAddRows="false" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="{DynamicResource championship}" Binding="{Binding Competition}"/>
<DataGridTextColumn Header="1" Binding="{Binding HomeWin}"/>
<DataGridTextColumn Header="X" Binding="{Binding Draw}"/>
</DataGrid.Columns>
Essentially I need to change the background of all the cell that have 1 and X header, if the value of the row is: < 50 the cell should have a background of red, if is >60 green.
What I did until now is:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="" Value="50">
</Trigger>
</Style.Triggers>
</Style>
But I doesn't found any property that bind the generic value each DataGridTextColumn.
How can I achieve this?
Thanks.
-
Oct 24th, 2016, 01:10 AM
#2
Re: Change datagrid cell color based on the value of the cell?
Hi,
I have a sample project here: Accessing Rows and Cells in a WPF DataGrid that will change the color of a cell using code instead of Triggers. Download the project and replace the SetColorCell() method with the snippet below.
This snippet will update the cell color in the third column if the value is Dept1.
vb.net Code:
Private Sub SetColorCell() BindUsingDataTable() For index = 0 To grid1.Items.Count - 1 Step 1 Dim firstRow As DataGridRow = grid1.GetRow(index) Dim cell As Controls.DataGridCell = TryCast(grid1.Columns(2).GetCellContent(firstRow).Parent, Controls.DataGridCell) 'set background If TryCast(cell.Content, TextBlock).Text = "Dept1" Then cell.Background = Brushes.Green End If Next End Sub
Note: You need to include the DataGridExtensions module in your project with the GetRow() function only.
Hope this will help.
- kgc
-
Oct 24th, 2016, 06:40 AM
#3
Thread Starter
New Member
Re: Change datagrid cell color based on the value of the cell?
Originally Posted by KGComputers
Hi,
I have a sample project here: Accessing Rows and Cells in a WPF DataGrid that will change the color of a cell using code instead of Triggers. Download the project and replace the SetColorCell() method with the snippet below.
This snippet will update the cell color in the third column if the value is Dept1.
vb.net Code:
Private Sub SetColorCell()
BindUsingDataTable()
For index = 0 To grid1.Items.Count - 1 Step 1
Dim firstRow As DataGridRow = grid1.GetRow(index)
Dim cell As Controls.DataGridCell = TryCast(grid1.Columns(2).GetCellContent(firstRow).Parent, Controls.DataGridCell)
'set background
If TryCast(cell.Content, TextBlock).Text = "Dept1" Then
cell.Background = Brushes.Green
End If
Next
End Sub
Note: You need to include the DataGridExtensions module in your project with the GetRow() function only.
Hope this will help.
- kgc
Thanks for the answer, but I'm a c# developer and also I need a pure xaml solution..
-
Oct 24th, 2016, 11:44 AM
#4
Re: Change datagrid cell color based on the value of the cell?
Thanks for the answer, but I'm a c# developer and also I need a pure xaml solution..
I dont' know if a solution can be achieved by pure XAML alone. A little help from IValueConverter will achieve that.
wpf-datagrid-trigger-on-cell-content
-
Oct 24th, 2016, 12:21 PM
#5
Re: Change datagrid cell color based on the value of the cell?
Ok, After doing some research, it can be done using DataGridTextColumn.CellStyle Trigger.
XAML Code:
<DataGridTextColumn.CellStyle> <Style TargetType="DataGridCell"> <Style.Triggers> <DataTrigger Binding="{Binding Path=Age}" Value="50"> <Setter Property="Background" Value="Gray"/> </DataTrigger> </Style.Triggers> </Style> </DataGridTextColumn.CellStyle>
:-D
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|