简体   繁体   中英

UserControl inside ContentControl

is it possible to insert some UserControl into ContentControl?

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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM