When a row is selected in my datagrid and a button is pressed, I want to change the FontWeight of the cells in that row to bold.
I've been looking for a way to do it, but all I can do is change the style of every columns, I can't find a way to get the selected row (or any rows for that matter).
There are no specific values that I can bind to from the ItemSource type, so a solution using XAML and a ValueConverter is unwanted due to the increased complexity. That is, unless it's the only way.
This is how I'm proceeding:
<DataGrid Name="dgSessions" Width="200" Height="100"
CanUserAddRows="False" CanUserDeleteRows="False"
HeadersVisibility="None" GridLinesVisibility="None" AutoGenerateColumns="False"
SelectionMode="Single" Background="White">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Binding="{Binding Path=Name}"></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Setters>
<Setter Property="FontWeight"
Value="Normal"/>
</Style.Setters>
</Style>
</DataGrid.CellStyle>
</DataGrid>
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
Style oldStyle = dgSessions.SelectedCells.First().Column.CellStyle;
Setter setter = null;
foreach (Setter item in oldStyle.Setters)
{
if (item.Property.Name == "FontWeight")
{
setter = new Setter(item.Property, FontWeights.Bold, item.TargetName);
break;
}
}
Style newStyle = new Style(oldStyle.TargetType);
newStyle.Setters.Add(setter);
dgSessions.SelectedCells.First().Column.CellStyle = newStyle;
}