简体   繁体   English

ContentControl中的UserControl

[英]UserControl inside ContentControl

is it possible to insert some UserControl into ContentControl? 是否可以将一些UserControl插入ContentControl?

but I need to dynamically decide which UserControl I need to insert (like with DataTemplateSelector). 但我需要动态决定我需要插入哪个UserControl(比如DataTemplateSelector)。

It is possible. 有可能的。 You need to have a ContentControl let's say like this one: 你需要有一个ContentControl让我们这样说:

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

And then you need to have your different UserControl like these two: 然后你需要像这两个不同的UserControl

<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: 如果你想以动态方式更改它们,你只需要以编程方式更改ContentMain ContentControl的内容:

// 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. 是的,您可以在ContentControl.Content放置任何对象,但是根据确定您想要的UserControl的内容,有多种方法可以实现此目的。

My personal favorite is to go with a DataTrigger that determines the ContentControl.ContentTemplate based on some condition 我个人最喜欢的是使用DataTrigger来根据某些条件确定ContentControl.ContentTemplate

Here's an example that bases the ContentControl.Content on a ComboBox's selected value: 这是一个将ContentControl.Content基于ComboBox所选值的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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