如何以样式设置 FlowDocument 的 Table.Columns

发布于 2024-09-09 19:24:21 字数 1542 浏览 3 评论 0原文

我有多个 FlowDocument,它们都有一个表。所有表格看起来都一样。
所以我想重构 FlowDocument 。
我的初始文档如下所示:

<FlowDocument xmlns=...>
  <Table>
    <Table.Columns>
      <TableColumn Width="12*" />  
      <TableColumn Width="1.5*" />
      <TableColumn Width="2*" />
      <TableColumn Width="*" />
      <TableColumn Width="2*" />
      <TableColumn Width="*" />
    </Table.Columns>
    <TableRowGroup>
      <TableRow>
        <TableCell>Some content...</TableCell>
        ...
  </Table>
</FlowDocument>  

我正在寻找类似的内容:

<FlowDocument xmlns=...>
  <FlowDocument.Resources>
     <Style TargetType="{x:Type Table}">
       <Setter Property="ColumnsDefinition">
         <Setter.Value>
           <ControlTemplate>
             <TableColumn Width="12*" />  
             <TableColumn Width="1.5*" />
             <TableColumn Width="2*" />
             <TableColumn Width="*" />
             <TableColumn Width="2*" />
             <TableColumn Width="*" />
           </ControlTemplate>
         </Setter.Value>
       </Setter>
     </Style> 
  </FlowDocument.Resources>
  <Table>
    <TableRowGroup>
      <TableRow>
        <TableCell>Some content...</TableCell>
        ...
  </Table>
</FlowDocument>  

但不幸的是 FlowDocuments 表没有属性 Template

I have multiple FlowDocuments, all of them have a table. All tables look the same.

So I want to refactor the FlowDocuments.

My initial document looks like:

<FlowDocument xmlns=...>
  <Table>
    <Table.Columns>
      <TableColumn Width="12*" />  
      <TableColumn Width="1.5*" />
      <TableColumn Width="2*" />
      <TableColumn Width="*" />
      <TableColumn Width="2*" />
      <TableColumn Width="*" />
    </Table.Columns>
    <TableRowGroup>
      <TableRow>
        <TableCell>Some content...</TableCell>
        ...
  </Table>
</FlowDocument>  

I'm looking for something like:

<FlowDocument xmlns=...>
  <FlowDocument.Resources>
     <Style TargetType="{x:Type Table}">
       <Setter Property="ColumnsDefinition">
         <Setter.Value>
           <ControlTemplate>
             <TableColumn Width="12*" />  
             <TableColumn Width="1.5*" />
             <TableColumn Width="2*" />
             <TableColumn Width="*" />
             <TableColumn Width="2*" />
             <TableColumn Width="*" />
           </ControlTemplate>
         </Setter.Value>
       </Setter>
     </Style> 
  </FlowDocument.Resources>
  <Table>
    <TableRowGroup>
      <TableRow>
        <TableCell>Some content...</TableCell>
        ...
  </Table>
</FlowDocument>  

But unfortunately the FlowDocuments Table doesn't have a property Template.

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

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

发布评论

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

评论(1

情绪少女 2024-09-16 19:24:21

不幸的是,Columns 属性是只读集合属性,因此您可以在 XAML 中添加它,但不能从 Setter 中设置它。解决此问题的一种方法是创建一个可以设置的附加属性,然后将值从附加属性传输到集合。例如:

public static class TableBehavior
{
    public static readonly DependencyProperty AttachedColumnsProperty =
        DependencyProperty.RegisterAttached(
        "AttachedColumns",
        typeof(IEnumerable<TableColumn>),
        typeof(TableBehavior),
        new PropertyMetadata(AttachedColumnsChangedCallback));

    public static void SetAttachedColumns(Table element, IEnumerable<TableColumn> value)
    {
        element.SetValue(AttachedColumnsProperty, value);
    }

    public static IEnumerable<TableColumn> GetAttachedColumns(Table element)
    {
        return (IEnumerable<TableColumn>)element.GetValue(AttachedColumnsProperty);
    }

    private static void AttachedColumnsChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var table = d as Table;
        if (table != null)
        {
            table.Columns.Clear();
            foreach (var column in (IEnumerable<TableColumn>)e.NewValue)
            {
                table.Columns.Add(column);
            }
        }
    }
}

然后在 XAML 中:

<FlowDocument.Resources>
    <Style TargetType="Table">
        <Setter Property="local:TableBehavior.AttachedColumns">
            <Setter.Value>
                <x:Array Type="TableColumn">
                    <TableColumn Width="12*" />
                    <TableColumn Width="1.5*" />
                    <TableColumn Width="2*" />
                    <TableColumn Width="*" />
                    <TableColumn Width="2*" />
                    <TableColumn Width="*" />
                </x:Array>
            </Setter.Value>
        </Setter>
    </Style>
</FlowDocument.Resources>

Unfortunately, the Columns property is a read-only collection property, so you can add to it in XAML but you can't set it from a Setter. One way around this problem is to create an attached property that you can set, and then transfer the values from the attached property to the collection. For example:

public static class TableBehavior
{
    public static readonly DependencyProperty AttachedColumnsProperty =
        DependencyProperty.RegisterAttached(
        "AttachedColumns",
        typeof(IEnumerable<TableColumn>),
        typeof(TableBehavior),
        new PropertyMetadata(AttachedColumnsChangedCallback));

    public static void SetAttachedColumns(Table element, IEnumerable<TableColumn> value)
    {
        element.SetValue(AttachedColumnsProperty, value);
    }

    public static IEnumerable<TableColumn> GetAttachedColumns(Table element)
    {
        return (IEnumerable<TableColumn>)element.GetValue(AttachedColumnsProperty);
    }

    private static void AttachedColumnsChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var table = d as Table;
        if (table != null)
        {
            table.Columns.Clear();
            foreach (var column in (IEnumerable<TableColumn>)e.NewValue)
            {
                table.Columns.Add(column);
            }
        }
    }
}

And then in XAML:

<FlowDocument.Resources>
    <Style TargetType="Table">
        <Setter Property="local:TableBehavior.AttachedColumns">
            <Setter.Value>
                <x:Array Type="TableColumn">
                    <TableColumn Width="12*" />
                    <TableColumn Width="1.5*" />
                    <TableColumn Width="2*" />
                    <TableColumn Width="*" />
                    <TableColumn Width="2*" />
                    <TableColumn Width="*" />
                </x:Array>
            </Setter.Value>
        </Setter>
    </Style>
</FlowDocument.Resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文