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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8959133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:22:27  来源:igfitidea点击:

How to show a data template on a content control?

c#wpftemplatesxamldatatemplate

提问by Darf Zon

Imagine that in one data template, I have a textBox, and another data template, I've got two textboxes.

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

According to this, in the view has a checkbox, and show each template.. is this possible?

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

Sorry if my question is so doubt, I've investigate it but I didn't find out.

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

I was did this, I know this is useless, but is only for testing.

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

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

采纳答案by Gayot Fow

Your design should include a template selector...

你的设计应该包括一个模板选择器......

DataTemplates are an extremely powerful part of WPF, and by using them, you can abstract all sorts of display code. However, there are times when they fall short - and initially when I was learning WPF I was disappointed by that. For instance, you only get to set one DataTemplate on an items control, and while that made sense, it felt limiting. What if I wanted to use different templates depending on the content of the item? Do I have to build all that logic into a single data template?

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

source: Switch on the code

来源: 打开代码

This is WPF's answer to your question and should produce the behaviour you are after. The tutorial has some lucid examples to show the technique...

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



Note: Alternate link at WPF Tutorial - How to use a Data Template Selector

注意:WPF 教程中的备用链接- 如何使用数据模板选择器

回答by JayP

Instead of setting the Templateproperty, try this:

而不是设置Template属性,试试这个:

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

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

回答by Андр?й Вандич

You can specify one of your templates on lower level. Something like:

您可以在较低级别指定模板之一。就像是:

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

回答by Eric Ouellet

I'm pretty late but I got the question and this is my working solution. Hope it could help other?

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

Please note than local:UserControlSpecialSignalTtrModel inherits from SignalProviderSpecial.

请注意 local:UserControlSpecialSignalTtrModel 继承自 SignalProviderSpecial。

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ParametricStudyAnalysis.ScopeSelection.Special"
             xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" x:Class="ParametricStudyAnalysis.ScopeSelection.Special.UserControlAddSpecialSignal"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <local:UserControlAddSpecialSignalModel></local:UserControlAddSpecialSignalModel>
    </UserControl.DataContext>

    <UserControl.Resources>
        <DataTemplate DataType="{x:Type local:UserControlSpecialSignalTtrModel}">
            <local:UserControlSpecialSignalTtr/>
        </DataTemplate>     
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>


        <GroupBox Header="Signal type" Grid.Row="0" Padding="5">
            <xcdg:DataGridControl Name="DataGrid" SelectionMode="Single" ItemsSource="{Binding SpecialSignalEntries}"
                              SelectedItem="{Binding SpecialSignalEntrySelected}" Height="200">
            <xcdg:DataGridControl.Columns>
                <xcdg:Column FieldName="Name" Title="Type of special signal" ReadOnly="True"></xcdg:Column>
            </xcdg:DataGridControl.Columns>
        </xcdg:DataGridControl>
        </GroupBox>

        <GroupBox Header="Parameters" Grid.Row="1" Margin="0,3,0,0" Padding="5">
            <ContentControl Name="MyContentControl" 
                            DataContext="{Binding SpecialSignalEntrySelected, Mode=OneWay}" 
                            Content="{Binding SignalProviderSpecial}">
            </ContentControl>
        </GroupBox>
    </Grid>
</UserControl>