【问题标题】:WPF DataGrid Trigger on cell contentWPF DataGrid 触发单元格内容
【发布时间】:2015-06-17 08:25:43
【问题描述】:

我有一个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>

【问题讨论】:

    标签: c# wpf datagrid triggers


    【解决方案1】:

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

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

    【讨论】:

    • 听起来不错,但我的对象的所有属性都需要它。我不想复制 12 次我确定有效的代码
    • 完美。您的编辑有效。所以我认为只有在 XAML 中没有办法做到这一点。对吗?
    • 我 99% 确定没有; )
    【解决方案2】:

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

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

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

    【讨论】:

    • 我有 12 列。我不想为所有列复制此代码。
    【解决方案3】:

    你可以这样做 -

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2016-06-14
      • 2018-06-05
      • 2023-01-13
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多