2010-07-14 4 views
1

J'ai plusieurs FlowDocument s, tous ont une table. Toutes les tables se ressemblent.
Donc, je veux refactoriser le FlowDocument s.
Mon document initial ressemble à:Comment définir les Table.Columns d'un FlowDocument dans un style

<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> 

Je cherche quelque chose comme:

<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> 

Mais malheureusement, le tableau ne FlowDocuments pas une propriété Template.

Répondre

2

Malheureusement, la propriété Columns est une propriété de collection en lecture seule, vous pouvez donc l'ajouter en XAML mais vous ne pouvez pas la définir à partir d'un Setter. Un moyen de contourner ce problème est de créer une propriété jointe que vous pouvez définir, puis de transférer les valeurs de la propriété jointe à la collection. Par exemple:

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); 
      } 
     } 
    } 
} 

Et puis en 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> 
+0

Merci! Votre solution proposée a fonctionné pour moi! – WaltiD