Reputation: 2546
I have a datagrid
than contains value comes from a stored procedure
. All values are set Bold
as FontWeight
.
I'd like to make the text normal when the cell content is equal to 0.
How can I do that with a trigger?
I've done it like below but it's not working:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<Trigger Property="Content" Value="0">
<Setter Property="FontWeight" Value="Normal"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
Upvotes: 9
Views: 24545
Reputation: 547
You could do this -
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content.Text, Mode=OneWay, RelativeSource={RelativeSource Self}}" Value="0">
<Setter Property="FontWeight" Value="Normal"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
Upvotes: 1
Reputation: 4808
You can't access DataGridCell.Content
that way, use a DataTrigger
instead based on your DataGrid.SelectedItem.YourProperty
like this:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<DataTrigger Binding="{Binding YourProperty}" Value="0">
<Setter Property="FontWeight" Value="Normal"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
EDIT:
Assuming your DataGridColumns
are text-based then you can use an IValueConverter
as below:
Note that if some of data-grid columns are not text-based, this solution still works for those columns which are.
Xaml:
<Window.Resources>
<local:FontWeightConverter x:Key="fontWeightConverter"/>
</Window.Resources>
...
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Setters>
<Setter Property="FontWeight"
Value="{Binding RelativeSource={RelativeSource Self},
Path=Content.Text,
Converter={StaticResource fontWeightConverter}}" />
</Style.Setters>
</Style>
</DataGrid.CellStyle>
Converter:
public class FontWeightConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (value != null && value.ToString() == "0")
return FontWeights.Normal;
return FontWeights.Bold;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Upvotes: 13
Reputation: 2620
This is a way to define that column:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding DataBaseValue}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
You can add a binding on the FontWeightof the TextBox with a converter associated to the Text if itself.
Upvotes: 2