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;
}
You can define the
DataGridRow
style as below and on button click set the property to trigger a notification to apply theFontWeight
on the RowThe DataGrid will be defined as
Now to integrate it, you have to define property in the model which is bound to
ItemSource
ofDataGrid
(The model should implement INotifyPropertyChanged interface). On button click, set the property defined in model and bound inDataTrigger
Turns out, you can get a row of a datagrid like that:
There is also a different method to get a row from an Item.
So I did the following to set the row I want in bold:
Retrieve the index with
int index = myObservableCollection.IndexOf(myObject)
I don't think the index is always valid if you have a lot of rows and virtualization is enabled, but given my context, it's fine.Create my Setter
Get my row:
Create Style and set it: