如何在内容控件上显示数据模板?

时间:2012-01-22 05:12:00

标签: c# wpf templates xaml datatemplate

想象一下,在一个数据模板中,我有一个textBox和另一个数据模板,我有两个文本框。

根据这个,在视图中有一个复选框,并显示每个模板..这可能吗?

对不起,如果我的问题是如此怀疑,我已经调查了,但我没有发现。

我这样做了,我知道这没用,但仅用于测试。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <DataTemplate DataType="{x:Type ContentControl}" x:Key="T1">
            <StackPanel>
                <TextBox Height="20" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate DataType="{x:Type ContentControl}" x:Key="T2">
            <StackPanel>
                <TextBox Height="20" />
                <TextBox Height="20" />
            </StackPanel>
        </DataTemplate>
    </Window.Resources>


    <Grid>
        <ContentControl Template="{StaticResource T1}" />
    </Grid>
</Window>

4 个答案:

答案 0 :(得分:25)

不要设置Template属性,请尝试以下方法:

<ContentControl ContentTemplate="{StaticResource T1}" />

答案 1 :(得分:6)

您可以在较低级别指定一个模板。 类似的东西:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <DataTemplate DataType="{x:Type ContentControl}" x:Key="T1">
            <StackPanel>
                <TextBox Height="20" />
            </StackPanel>
        </DataTemplate>
    </Window.Resources>


    <Grid>
        <ContentControl Template="{StaticResource T1}">
        <ContentControl.Resources>
        <DataTemplate DataType="{x:Type ContentControl}" x:Key="T2">
            <StackPanel>
                <TextBox Height="20" />
                <TextBox Height="20" />
            </StackPanel>
        </DataTemplate>
        <ContentControl.Resources>
        </ContentControl>
    </Grid>
</Window>

答案 2 :(得分:3)

您的设计应包含模板选择器......

  

DataTemplates是WPF中非常强大的一部分,通过使用它们,您可以抽象出各种显示代码。然而,有些时候它们不足 - 而且最初在我学习WPF时我对此感到失望。例如,你只能在一个项目控件上设置一个DataTemplate,虽然这是有意义的,但它感觉有限。如果我想根据项目的内容使用不同的模板怎么办?我是否必须将所有逻辑构建到单个数据模板中?

来源:Switch on the code

这是WPF对你的问题的回答,应该产生你所追求的行为。本教程有一些清晰的例子来展示这种技术......


注意:WPF Tutorial - How to use a Data Template Selector

处的备用链接

答案 3 :(得分:0)

我已经很晚了,但我得到了一个问题,这是我的工作解决方案。希望它可以帮助其他?

请注意本地:UserControlSpecialSignalTtrModel继承自SignalProviderSpecial。

nSample <- 100
patient_id <- rep(c(1, 2, 1, 1, 3, 2, 1, 4, 5, 6), nSample)
visit_id <- 1:nSample    
visit_date <- rep(as.Date(c('2016-12-02', '2016-12-02', '2016-12-30',
'2016-12-15', '2016-12-30', '2017-02-01',
'2017-02-15', '2017-02-10', '2017-01-15', '2017-03-01')), nSample)
df <- data.frame(visit_id, patient_id, visit_date)

opc3 <- function(df) {
    df[, visit_date20 := visit_date - 20 * 7] # Create a 20 weeks boundry 

    ## Count previous visits within the range
    df[df, .(visits = .N),
       on = .(patient_id, visit_date < visit_date, visit_date > visit_date20),
       by = .EACHI]
}

dt <- data.table(df)
dt3 <- copy(dt)[, visit_date := as.IDate(visit_date)] # Convert visit_date to a proper Date class

library(microbenchmark)
op <- microbenchmark(
    OP1 = copy(dt)[, visits := visitFreq(visit_date), by = patient_id],
    OP2 = copy(dt)[, visits := .(list(visit_date)), by = patient_id][, visits := mapply(calc, visits, visit_date)],
    OP3 = opc3(copy(dt3)),
    times = 10L)
    print(op)