7

is it possible to insert some UserControl into ContentControl?

but I need to dynamically decide which UserControl I need to insert (like with DataTemplateSelector).

2
  • 1
    Of course it is. But your decision will be based on what data? Commented Jan 24, 2013 at 16:50
  • on bounded content of ContentControl (Content={Binding...}) Commented Jan 24, 2013 at 16:58

2 Answers 2

18

It is possible. You need to have a ContentControl let's say like this one:

<ContentControl Name="ContentMain" Width="Auto" Opacity="1" Background="Transparent" ></ContentControl> 

And then you need to have your different UserControl like these two:

<UserControl x:Class="MyNamespace.UserControl1" 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" > <Grid Margin="5,5,5,10" > <Label Name="labelContentOne" VerticalAlignment="Top" FontStretch="Expanded" /> </Grid> 

<UserControl x:Class="MyNamespace.UserControl2" 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" > <Grid Margin="5,5,5,10" > <Label Name="labelContentTwo" VerticalAlignment="Top" FontStretch="Expanded" /> </Grid> 

If you want to change them dinamically you only need to change the Content of the ContentMain ContentControl programatically:

// Initialize the content UserControl1 u1 = new UserControl1(); ContentMain.Content = u1; // Let's say it changes on a button click (for example) private void ButtonChangeContent_Click(object sender, RoutedEventArgs e) { UserControl2 u2 = new UserControl2(); ContentMain.Content = u2; } 

More or less that's the idea... ;)

Sign up to request clarification or add additional context in comments.

Comments

13

Yes, you can place any object in ContentControl.Content, however depending on what determines what UserControl you want, there are multiple ways of accomplishing this.

My personal favorite is to go with a DataTrigger that determines the ContentControl.ContentTemplate based on some condition

Here's an example that bases the ContentControl.Content on a ComboBox's selected value:

<DataTemplate DataType="{x:Type DefaultTemplate}"> <TextBlock Text="Nothing Selected" /> </DataTemplate> <DataTemplate DataType="{x:Type TemplateA}"> <localControls:UserControlA /> </DataTemplate> <DataTemplate DataType="{x:Type TemplateB}"> <localControls:UserControlB /> </DataTemplate> <Style TargetType="{x:Type ContentControl}" x:Key="MyContentControlStyle"> <Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}" /> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=MyComboBox, Path=SelectedValue}" Value="A"> <Setter Property="ContentTemplate" Value="{StaticResource TemplateA}" /> </DataTrigger> <DataTrigger Binding="{Binding ElementName=MyComboBox, Path=SelectedValue}" Value="B"> <Setter Property="ContentTemplate" Value="{StaticResource TemplateB}" /> </DataTrigger> </Style.Triggers> </Style> 

1 Comment

thanks, actually funny situation. I simply forgot that I can insert UserControl inside DataTemplate (like you did) and use standard ContentTemplateSelector.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.