WPF DataGridTemplateColumn 共享模板? [英] WPF DataGridTemplateColumn shared template?

查看:22
本文介绍了WPF DataGridTemplateColumn 共享模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 datagrid,它有许多 datagridtemplate 列,除了它们每个在模板的堆栈面板上都有不同的 datacontext 之外,这些列都是相同的.

Hi I have a datagrid that has a number of datagridtemplate columns that are all identical apart from they each have a different datacontext on the template's stackpanel.

<toolkit:DataGridTemplateColumn Header="Col 1">
                <toolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel DataContext="{Binding Times[0]}">
                        <!-- the structure that I want to extract to a template -->
                        </StackPanel>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

<toolkit:DataGridTemplateColumn Header="Col 2">
                <toolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel DataContext="{Binding Times[1]}">
                        <!-- the same structure here -->
                        </StackPanel>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

我想让每一列都使用一个特定的 itemtemplate(就像我对列表框所做的那样),但除非我遗漏了一些东西,否则我似乎看不到如何.

I want to have each column use a specific itemtemplate (like I've done with a listbox) but can't seem to see how unless I'm missing something.

推荐答案

您可以使用 ContentPresenter 为每列实例化一个 DataTemplate:

You could use a ContentPresenter to instantiate a DataTemplate for each column:

<toolkit:DataGrid.Resources>
    <DataTemplate x:Key="ColumnTemplate">
        <StackPanel>
            <!-- the structure that I want to extract to a template -->
        </StackPanel>
    </DataTemplate>
</toolkit:DataGrid.Resources>
<toolkit:DataGrid.Columns>
    <toolkit:DataGridTemplateColumn Header="Col 1">
        <toolkit:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ContentPresenter ContentTemplate="{StaticResource ColumnTemplate}" Content="{Binding Times[0]}"/>
            </DataTemplate>
        </toolkit:DataGridTemplateColumn.CellTemplate>
    </toolkit:DataGridTemplateColumn>
    <toolkit:DataGridTemplateColumn Header="Col 2">
        <toolkit:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ContentPresenter ContentTemplate="{StaticResource ColumnTemplate}" Content="{Binding Times[1]}"/>
            </DataTemplate>
        </toolkit:DataGridTemplateColumn.CellTemplate>
    </toolkit:DataGridTemplateColumn>
</toolkit:DataGrid.Columns>

如果 Times 的元素都是相同的类型,你也可以做 然后你就不需要指定 每列上的 ContentTemplate="{StaticResource ColumnTemplate}".

If the elements of Times are all the same type, you could also do <DataTemplate DataType={x:Type YourType}> and then you wouldn't need to specify ContentTemplate="{StaticResource ColumnTemplate}" on each column.

这篇关于WPF DataGridTemplateColumn 共享模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆