WPF DataGrid 触发单元格内容

WPF DataGrid Trigger on cell content

我有一个 datagrid 而不是包含来自 stored procedure 的值。所有值都设置为 BoldFontWeight

我想在单元格内容等于0时让文字正常显示

我怎样才能用触发器做到这一点?

我已经按照下面的方式完成了,但它不起作用:

<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>

这是定义该列的一种方法:

  <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                   <StackPanel Orientation="Horizontal">
                            <TextBox Text="{Binding DataBaseValue}"/>
                   </StackPanel>
             </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>

您可以在 TextBox 的 FontWeight 上添加绑定,并使用与 Text if 本身关联的转换器。

您无法通过这种方式访问​​ DataGridCell.Content,请根据您的 DataGrid.SelectedItem.YourProperty 使用 DataTrigger,如下所示:

    <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>

编辑:

假设您的 DataGridColumns 是基于文本的,那么您可以使用如下 IValueConverter

请注意,如果某些数据网格列不是基于文本的,则此解决方案仍然适用于那些基于文本的列。

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>

转换器:

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();
    }
}

你可以这样做 -

<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>