i need to move to wpf a windows forms application. I've some problem making the same style of DataGridView to Wpf DataGrid.
DataGridView is populated with a List through the DataSource property.
public class GameItem
{
public string Name { get; set; }
public bool Selected { get; set; }
}
When Selected = True the entire row on the DGV is Red, otherwise GreenYellow. SelectionColor is always the same of standard color, so moving between rows does not change any color.
this is DefaultCellStyle:
dataGridViewCellStyle4.BackColor = System.Drawing.Color.GreenYellow;
dataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle4.SelectionBackColor = System.Drawing.Color.GreenYellow;
dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.ControlText;
void dgvDetail_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0)
{
var gameItem = (sender as DataGridView).Rows[e.RowIndex].DataBoundItem as GameItem;
if (gameItem != null && gameItem.Selected)
{
e.CellStyle.SelectionForeColor = Color.White;
e.CellStyle.SelectionBackColor = Color.Red;
e.CellStyle.ForeColor = Color.White;
e.CellStyle.BackColor = Color.Red;
}
}
}
This is the Grid's definition in wpf application:
<Window x:Class="TestGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestGrid"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
<Style TargetType="DataGrid">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</Style.Resources>
<Setter Property="FontSize" Value="10pt" ></Setter>
<Setter Property="FontFamily" Value="Microsoft Sans Serif" ></Setter>
<Setter Property="FontWeight" Value="Bold" ></Setter>
<Setter Property="SelectionUnit" Value="FullRow" ></Setter>
<Setter Property="HeadersVisibility" Value="Row" ></Setter>
<Setter Property="AutoGenerateColumns" Value="False" ></Setter>
</Style>
</Window.Resources>
<Grid>
<DataGrid Name="dg1">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="GreenYellow"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="Auto" />
<DataGridTextColumn Header="Manufacturer" Binding="{Binding Manufacturer}" Width="*" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
i've this problems:
in wpf i dont understand how to show the row indicator arrow into row header.
in wpf when the user click on a row that become selected and change color, i dont want this behavoir. I want a row is red only when the bounded Selected item Property is true. So clicking a row it mustn't change color, backcolor have to stay on green and foreground white is Selected=false in data source.
this is the windows forms version:
this is wpf, row 1 and 3 are correctly drawed cause the binded item have Selected=True. The problem is when a row is clicked (1943 in the picture) it became highlighted even if Selected is false, i want to avoid the active row became highlighted like the windows forms version.
to disable red highlight on mouse selection remove HighlightBrushKey resources (or at least change thier Color to see the difference)
to change row headers create a Style for DataGridRowHeader type:
<Style TargetType="{x:Type DataGridRowHeader}" BasedOn="{StaticResource {x:Type DataGridRowHeader}}">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="0,0,1,1"/>
<Setter Property="Background" Value="GreenYellow"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<Trigger Property="IsRowSelected" Value="True">
<Setter Property="Content" Value="▸"/>
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
Row header is Red when Selected property is true, GreenYellow otherwise. It displays black right-pointing small triangle char when row is selected by used input.
or maybe even hide row headers and use DataGridTemplateColumn instead where selected cell will display >
modified Row and cell styles to change selection foreground
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="GreenYellow"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>