Search code examples
c#xamltemplatesdatatemplatexmltable

How to add a Template/DataTemplate to a TableCell


I googled around but I did't find any really useful stuff; my purpose is to display a specified DataTemplate for a specified Class:

    <FlowDocumentScrollViewer>
        <FlowDocument>
            <Table BorderBrush="Black" BorderThickness="1" CellSpacing="0">
                <Table.Columns>
                    <TableColumn></TableColumn>
                    <TableColumn></TableColumn>
                    <TableColumn></TableColumn>
                    <TableColumn></TableColumn>
                </Table.Columns>
                <Table.RowGroups>
                    <TableRowGroup >
                        <TableRow>
                            <TableCell>DataTemplate1</TableCell>//class1
                            <TableCell>DataTemplate2</TableCell>//class2
                            <TableCell>DataTemplate3</TableCell>//class3
                            <TableCell>DataTemplate4</TableCell>//class4
                        </TableRow>
                    </TableRowGroup> 
                </Table.RowGroups>
            </Table>
        </FlowDocument>
    </FlowDocumentScrollViewer>

Important it must be a XAML only solution because I load this xaml per XamlReader.Load() so there wont be a codebehind file.


Solution

  • For each type that you need a template for you can define a DataTemplate with just a DataType attribute somewhere in Resources. To get them to show up you'll need to then bind the data items to some ContentControl in your cells. Here's an example with templates for int and bool and bindings to collection items:

    <FlowDocumentScrollViewer>
      <FlowDocumentScrollViewer.Resources>
        <DataTemplate DataType="{x:Type system:Int32}">
          <TextBlock Text="{Binding StringFormat='A number: {0}'}" />
        </DataTemplate>
        <DataTemplate DataType="{x:Type system:Boolean}">
          <TextBlock Text="{Binding StringFormat='A bool: {0}'}" />
        </DataTemplate>
      </FlowDocumentScrollViewer.Resources>
      <FlowDocument>
        <Table BorderBrush="Black"
               BorderThickness="1"
               CellSpacing="0">
          <Table.Columns>
            <TableColumn></TableColumn>
            <TableColumn></TableColumn>
            <TableColumn></TableColumn>
            <TableColumn></TableColumn>
          </Table.Columns>
          <Table.RowGroups>
            <TableRowGroup>
              <TableRow>
                <TableCell>
                  <BlockUIContainer>
                    <ContentControl Content="{Binding MixedTypeList[0]}" />
                  </BlockUIContainer>
                </TableCell>
                <TableCell>
                  <BlockUIContainer>
                    <ContentControl Content="{Binding MixedTypeList[1]}" />
                  </BlockUIContainer>
                </TableCell>
                <TableCell>
                  <BlockUIContainer>
                    <ContentControl Content="{Binding MixedTypeList[2]}" />
                  </BlockUIContainer>
                </TableCell>
              </TableRow>
            </TableRowGroup>
          </Table.RowGroups>
        </Table>
      </FlowDocument>
    </FlowDocumentScrollViewer>