【问题标题】:How to show a data template on a content control?如何在内容控件上显示数据模板?
【发布时间】:2012-02-16 01:33:06
【问题描述】:

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

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

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

我是这样做的,我知道这没用,但只是为了测试。

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

【问题讨论】:

  • 太棒了。你提到的复选框在哪里?
  • @GarryVass 是的,我没有这样做。我首先以手动方式将模板实现到内容控件..目前的复选框不是问题..

标签: c# wpf templates xaml datatemplate


【解决方案1】:

不要设置Template 属性,试试这个:

&lt;ContentControl ContentTemplate="{StaticResource T1}" /&gt;

【讨论】:

  • 太棒了,这行得通!一个问题,要使复选框可以更改模板,我应该使用数据模板选择器吗?
  • 抱歉,我不是模板选择器方面的专家。也许 Gary Vass 的回答会让你走上正轨。
【解决方案2】:

您可以在较低级别指定您的模板之一。 比如:

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

【讨论】:

    【解决方案3】:

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

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

    来源:Switch on the code

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


    注意:WPF Tutorial - How to use a Data Template Selector 的备用链接

    【讨论】:

    • 感谢 Gary,这对我有用,感谢 orient me.. 明天我会发布解决方案
    • @DarfZon,一切都好,无需发布解决方案。
    • @Vikram 我很感激你抬起头来。从4年前开始,我想这是不可避免的。你可以试试我刚刚编辑的答案吗?
    • @Vikram 如果你得到一个非常好的结果,考虑回到这里给出答案,当这个人来自可靠的经验时,人们可以受益
    • 这就是为什么你永远应该在 StackOverflow 答案中添加链接:他们死了,因此这个答案现在完全没用了。
    【解决方案4】:

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

    请注意本地: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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2017-11-29
      相关资源
      最近更新 更多