Wpf 如何在样式中设置FlowDocument的Table.Columns

Wpf 如何在样式中设置FlowDocument的Table.Columns,wpf,flowdocument,Wpf,Flowdocument,我有多个FlowDocuments,它们都有一个表。所有表格看起来都一样。 所以我想重构FlowDocuments 我的初始文档如下所示: <FlowDocument xmlns=...> <Table> <Table.Columns> <TableColumn Width="12*" /> <TableColumn Width="1.5*" /> <TableColumn Wi

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

<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表没有属性
模板

不幸的是,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);
            }
        }
    }
}
公共静态类TableBehavior
{
公共静态只读从属属性AttachedColumnsProperty=
DependencyProperty.RegisterAttached(
“附件列”,
类型(IEnumerable),
类型(表行为),
新属性元数据(附件列SchangedCallback));
公共静态void SetAttachedColumns(表元素,IEnumerable值)
{
元素设置值(AttachedColumnsProperty,value);
}
公共静态IEnumerable GetAttachedColumns(表元素)
{
返回(IEnumerable)元素.GetValue(AttachedColumnsProperty);
}
私有静态void AttachedColumnShangedCallback(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var表=d为表;
如果(表!=null)
{
table.Columns.Clear();
foreach(在(IEnumerable)e.NewValue中的var列)
{
表.列.添加(列);
}
}
}
}
然后在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>