wpf 内容控件中的用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14506374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:13:08  来源:igfitidea点击:

UserControl inside ContentControl

c#wpf

提问by Vlad

is it possible to insert some UserControl into ContentControl?

是否可以在 ContentControl 中插入一些 UserControl?

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

但我需要动态决定我需要插入哪个 UserControl(就像使用 DataTemplateSelector)。

回答by Sonhja

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

有可能的。你需要有一个ContentControl让我们这样说的:

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

And then you need to have your different UserControllike 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 ContentMainContentControl 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... ;)

或多或少就是这个想法......;)

回答by Rachel

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 DataTriggerthat determines the ContentControl.ContentTemplatebased on some condition

我个人最喜欢的是根据某些条件DataTrigger确定ContentControl.ContentTemplate

Here's an example that bases the ContentControl.Contenton 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>