在 WPF DataGrid 的各个单元格上设置删除线的最佳方法?

发布于 2024-11-02 15:07:51 字数 291 浏览 0 评论 0原文

在 WPF DataGrid 的各个单元格上将字体设置为删除线样式的最佳(简单)方法是什么?

...

我知道的选项是在单个单元格中插入 TextBlock 控件或使用 DataGridTemplateColumn - 并使用其中的 TextDecorations 属性。无论哪种方式,这都是一项艰巨的任务,我想使用 DataGrid 的默认自动生成列功能,特别是因为我的 ItemsSource 是 DataTable。

另外,有没有办法访问使用默认 DataGridTextColumn 生成的 TextBlock?

What is the best (easy) way to set the font to a strikethrough style on individual cells of the WPF DataGrid?

...

Options that I'm aware of are inserting TextBlock controls in individual cells or using a DataGridTemplateColumn - and using the TextDecorations property therein. Either way this is quite a mission, I'd like to use the default AutoGenerate Columns function of DataGrid, especially since my ItemsSource is a DataTable.

As and aside, is there any way to access the TextBlock generated using the default DataGridTextColumn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

檐上三寸雪 2024-11-09 15:07:51
<DataGridTextColumn Binding="{Binding Name}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextDecorations" Value="Strikethrough"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

当然,您可以将 setter 包装在 DataTrigger 中以选择性地使用它。

<DataGridTextColumn Binding="{Binding Name}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextDecorations" Value="Strikethrough"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

Of course you can wrap the setter in a DataTrigger to use it selectively.

半葬歌 2024-11-09 15:07:51

如果要根据特定单元格绑定删除线,则会遇到绑定问题,因为 DataGridTextColumn.Binding 仅更改 TextBox.Text 的内容。如果 Text 属性的值就是您所需要的,您可以绑定到 TextBox 本身:

<Setter Property="TextDecorations" 
  Value="{Binding RelativeSource={RelativeSource Self}, 
  Path=Text, 
  Converter={StaticResource TextToTextDecorationsConverter}}" />

但是如果您想要绑定到与 TextBox.Text 不同的内容,则必须通过 DataGridRow 进行绑定,DataGridRow 是 TextBox 中的父级。视觉树。 DataGridRow 有一个 Item 属性,可以访问用于整行的完整对象。

<Setter Property="TextDecorations" 
  Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, 
  Path =Item.SomeProperty, 
  Converter={StaticResource SomePropertyToTextDecorationsConverter}}" />

转换器看起来像这样,假设某些东西是布尔类型:

public class SomePropertyToTextDecorationsConverter: IValueConverter {
  public object Convert(object value, Type targetType, object parameter, 
    CultureInfo culture) 
  {
      if (value is bool) {
        if ((bool)value) {
          TextDecorationCollection redStrikthroughTextDecoration =
            TextDecorations.Strikethrough.CloneCurrentValue();
          redStrikthroughTextDecoration[0].Pen = 
            new Pen {Brush=Brushes.Red, Thickness = 3 };
          return redStrikthroughTextDecoration; 
        }
      }
      return new TextDecorationCollection(); 
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
      CultureInfo culture) 
    {
      throw new NotImplementedException();
    }
  }

If you want to bind the strikethrough based on a particular cell, you have a binding problem, because the DataGridTextColumn.Binding changes only the content of TextBox.Text. If the value of the Text property is all you need you can bind to the TextBox itself:

<Setter Property="TextDecorations" 
  Value="{Binding RelativeSource={RelativeSource Self}, 
  Path=Text, 
  Converter={StaticResource TextToTextDecorationsConverter}}" />

But if you want to bind to something different than TextBox.Text, you have to bind through the DataGridRow, which is a parent of the TextBox in the visual tree. The DataGridRow has an Item property, which gives access to the complete object used for the whole row.

<Setter Property="TextDecorations" 
  Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, 
  Path =Item.SomeProperty, 
  Converter={StaticResource SomePropertyToTextDecorationsConverter}}" />

The converter looks like this, assuming the something is of type boolean:

public class SomePropertyToTextDecorationsConverter: IValueConverter {
  public object Convert(object value, Type targetType, object parameter, 
    CultureInfo culture) 
  {
      if (value is bool) {
        if ((bool)value) {
          TextDecorationCollection redStrikthroughTextDecoration =
            TextDecorations.Strikethrough.CloneCurrentValue();
          redStrikthroughTextDecoration[0].Pen = 
            new Pen {Brush=Brushes.Red, Thickness = 3 };
          return redStrikthroughTextDecoration; 
        }
      }
      return new TextDecorationCollection(); 
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
      CultureInfo culture) 
    {
      throw new NotImplementedException();
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文