views:

25

answers:

1

Hi

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.

+1  A: 

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>
Quartermeister
Thank you! Your proposed solution did work for me!
WaltiD