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

时间:2010-07-14 05:59:26

标签: wpf flowdocument

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

1 个答案:

答案 0 :(得分:2)

不幸的是,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>