How to show data template in content control?

Imagine that in one data template I have a text field and another data template, I have two text fields.

In accordance with this, there is a flag in the view and each template is shown .. is this possible?

Sorry if my question is so in doubt, I studied it, but I did not recognize it.

I did it, I know that it is useless, but 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> 
+8
source share
4 answers

Your design should include a template selector ...

DataTemplates are an extremely powerful part of WPF, and using them you can abstract all kinds of displayed code. However, there are times when they do not fit - and initially, when I studied WPF, I was disappointed with this. For example, you can only set one DataTemplate element in an element control, and although that makes sense, it becomes limited. What if I wanted to use different templates depending on the content of the element? Should I build all this logic in one data template?

source: Include code

This is WPF's answer to your question and should lead to the behavior you are after. The tutorial has some clear examples to show the technique ...


Note: Alternative WPF Tutorial Link - How to Use the Data Template Selector

+2
source

Instead of setting the Template property, try the following:

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

+26
source

You can specify one of your templates at a 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> 
+6
source

I'm quite late, but I have a question, and this is my working solution. Hope this can help others?

Note that local: UserControlSpecialSignalTtrModel is inherited from 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> 
0
source

All Articles