When I select a row in the DataGrid and then a button is clicked, the color of the selected row changes, I use Visual Studio 2013 and it's Wpf application.
When I select a row in the DataGrid and then a button is clicked, the color of the selected row changes, I use Visual Studio 2013 and it's Wpf application.
This code snippet changes the color of the selected row from the DataGrid myDataGrid
to red.
int selectedIndex = myDataGrid.SelectedIndex;
//Guarda a Row selecionada
DataGridRow row =
myDataGrid.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as DataGridRow;
if(row == null)//A linha selecionada não está visivel
{
object item = myDataGrid.Items[selectedIndex];
myDataGrid.ScrollIntoView(item);//Torna a linha selecionada visivel
row = myDataGrid.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as DataGridRow;
}
//Altera a cor para vermelho
row.Background = Brushes.Red;
Note:
The SelectionUnit property of myDataGrid
must be as FullRow
You can also do validations in WPF, using datatriggers
. Note that this code snippet is located in Application.xaml , inside the Application.Resources
p>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Tipo}" Value="Erro">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.2"/>
<GradientStop Color="#ffb1b1" Offset="0.85"/>
<GradientStop Color="WhiteSmoke" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Tipo}" Value="Aviso">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.2"/>
<GradientStop Color="#ffee86" Offset="0.85"/>
<GradientStop Color="WhiteSmoke" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Tipo}" Value="Info">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.2"/>
<GradientStop Color="#c6deff" Offset="0.85"/>
<GradientStop Color="WhiteSmoke" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Tipo}" Value="Sucesso">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.2"/>
<GradientStop Color="#c6ffc7" Offset="0.85"/>
<GradientStop Color="WhiteSmoke" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
</Style>
In this example, the applied filters went to the Type column. The value
tag is assigned to, for example: If the line has the Error value in the column, then the line will be in the color you assign. If you do not master WPF, you you can create% other than% I created in the example, which applies to the setters
Background, such as:
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="1"/>
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="11px"/>
<Setter Property="FontWeight" Value="Normal"/>
For more information on this Styles / Templates, click here