[英]How to show a data template on a content control?
Imagine that in one data template, I have a textBox, and another data template, I've got two textboxes. 想象一下,在一个数据模板中,我有一个textBox和另一个数据模板,我有两个文本框。
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>
不要设置Template
属性,请尝试以下方法:
<ContentControl ContentTemplate="{StaticResource T1}" />
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>
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.
DataTemplates是WPF中非常强大的一部分,通过使用它们,您可以抽象出各种显示代码。 However, there are times when they fall short - and initially when I was learning WPF I was disappointed by that.
然而,有些时候它们不足 - 而且最初在我学习WPF时我对此感到失望。 For instance, you only get to set one DataTemplate on an items control, and while that made sense, it felt limiting.
例如,你只能在一个项目控件上设置一个DataTemplate,虽然这是有意义的,但它感觉有限。 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?
我是否必须将所有逻辑构建到单个数据模板中?
source: Switch on the code 来源: 打开代码
This is WPF's answer to your question and should produce the behaviour you are after. 这是WPF对你的问题的回答,应该产生你所追求的行为。 The tutorial has some lucid examples to show the technique...
本教程有一些清晰的例子来展示这种技术......
Note: Alternate link at WPF Tutorial - How to use a Data Template Selector 注意: WPF教程中的备用链接- 如何使用数据模板选择器
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. 请注意比本地: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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.