1

複数FlowDocumentの があり、すべてにテーブルがあります。すべてのテーブルは同じように見えます。
だから私はsをリファクタリングしたい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

4

1 に答える 1

2

残念ながら、Columns プロパティは読み取り専用のコレクション プロパティであるため、XAML で追加することはできますが、Setter から設定することはできません。この問題を回避する 1 つの方法は、設定できる添付プロパティを作成し、添付プロパティからコレクションに値を転送することです。例えば:

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>
于 2010-07-14T11:54:57.407 に答える