How to show a data template on a content control?
Asked Answered
N

4

12

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>
Novelty answered 22/1, 2012 at 5:12 Comment(2)
Great. And where's the check box you referred to?Linoleum
@GarryVass Yes, I haven't done that. I was doing first to implement in a manual way the template to the content control.. the checkBox for the moment is not a problem..Novelty
L
2

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?

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


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

Linoleum answered 22/1, 2012 at 6:15 Comment(5)
Thanks Gary, this works for me, thanks to orient me.. tomorrow I will post the solutionNovelty
@DarfZon, all's well, there's no need to post a solution.Linoleum
@Vikram I am grateful for the head's up. From 4+ years ago I suppose it's inevitable. Can you try the alternate I just edited in to the answer?Linoleum
@Vikram if you get a really good result, consider returning here to give an answer, people can benefit when the person is comming from solid experienceLinoleum
This is why you never should add links in a StackOverflow answer: They die, hence this answer is now totally useless.Kuvasz
J
33

Instead of setting the Template property, try this:

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

Jennings answered 22/1, 2012 at 5:56 Comment(2)
great, this works! one question, to do that the checkbox can change the template, should I use data template selector?Novelty
Sorry but I'm no expert on Template Selectors. Maybe Gary Vass' answer will get you on the right track with that.Jennings
L
6

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>
Landwehr answered 2/8, 2012 at 18:50 Comment(0)
L
2

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?

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


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

Linoleum answered 22/1, 2012 at 6:15 Comment(5)
Thanks Gary, this works for me, thanks to orient me.. tomorrow I will post the solutionNovelty
@DarfZon, all's well, there's no need to post a solution.Linoleum
@Vikram I am grateful for the head's up. From 4+ years ago I suppose it's inevitable. Can you try the alternate I just edited in to the answer?Linoleum
@Vikram if you get a really good result, consider returning here to give an answer, people can benefit when the person is comming from solid experienceLinoleum
This is why you never should add links in a StackOverflow answer: They die, hence this answer is now totally useless.Kuvasz
B
1

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.

<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>
Bur answered 13/3, 2017 at 21:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.