简体   繁体   English

如何将Template / DataTemplate添加到TableCell

[英]How to add a Template/DataTemplate to a TableCell

I googled around but I did't find any really useful stuff; 我用Google搜索,但我找不到任何有用的东西; my purpose is to display a specified DataTemplate for a specified Class : 我的目的是为指定的Class显示指定的DataTemplate

    <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. 重要的是它必须是仅XAML解决方案,因为我按照XamlReader.Load()加载此xaml,因此不会有代码隐藏文件。

For each type that you need a template for you can define a DataTemplate with just a DataType attribute somewhere in Resources. 对于您需要模板的每种类型,您可以在参考资料中的某处定义一个DataTemplate,其中只有一个DataType属性。 To get them to show up you'll need to then bind the data items to some ContentControl in your cells. 要让它们显示,您需要将数据项绑定到单元格中的某些ContentControl。 Here's an example with templates for int and bool and bindings to collection items: 这是一个使用int和bool模板以及绑定到集合项的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM