I’m using the MVVM Light Toolkit and I have a DataGrid bound to an ObservableCollection. There is only one text column displayed. I’d like the text of the cell to be Bold or Normal depending on a boolean that is inside the object displayed. I figured I could use RelayCommands but they only take 1 parameter and I need at least 2 to get the CellContent (the DataGridRowEventArgs and the DataGrid itself). I tried to fire a RelayCommand Execute delegate on “LoadingRow” event but with only one parameter I couldn’t do it.
Here is the DataGrid in the XAML:
<DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Margin="112,34,0,8" Width="100" IsReadOnly="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" ItemsSource="{Binding CurrentNewsList}" AutoGenerateColumns="False" SelectedIndex="0">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Title}" MinWidth="92" Width="Auto" FontFamily="Segoe UI" Foreground="Black" FontWeight="{Binding CurrentNewsList[0].MyFont}"/>
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<Custom:EventToCommand Command="{Binding NewsSelectedCommand}" CommandParameter="{Binding SelectedIndex, ElementName=dataGrid1}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
I set the grid in Blend. Notice that the FontWeight is bound like “{Binding CurrentNewsList[0].MyFont}”. Is it right ? I also tried “{Binding MyFont}” but both got the same result: No BOld 🙁
MyFont is set in the Object constructor with a boolean:
MyFont = newIsRead ? FontWeights.Normal : FontWeights.Bold;
Please help.
Thx
You could just use an implicit style and a trigger:
(If you ever have more columns you could just use the styles on the column (
ElementStyle&ElementEditingStyle) instead to limit the effect)