在另一个UserControl中嵌入WPF UserControl

13

我想在XAML中将一个UserControl设置为另一个UserControlContent,就像你可以将ButtonContent设置为任何内容一样。

假设我的"外部" UserControl看起来像这样:

<MyUserControl>
   <Grid>
      <Border FancyPantsStyling="True">

         <-- I want to insert other controls here -->

      </Border>
   </Grid>
</MyUserControl>

我想以以下方式进行实例化:

<local:MyUserControl>
   <local:MyUserControl.Content>
      <local:AnotherControl />
   </local:MyUserControl.Content>
</local:MyUserControl>

如何设计MyUserControl,以便在特定位置呈现其 Content

3个回答

7
你在UserControl的XAML中放置的所有内容都是它的Content,因此你不能通过设置Content属性来注入其他内容。你可以使用几种不同的方法来处理此问题。如果你没有MyUserControl代码后台,可以将其删除并使用以下方式:
<ContentControl>
    <ContentControl.Template>
        <ControlTemplate TargetType="{x:Type ContentControl}">
            <Grid>
                <Border FancyPantsStyling="True">
                    <ContentPresenter/>
                </Border>
            </Grid>
        </ControlTemplate>
    </ContentControl.Template>

    <local:AnotherControl/>
</ContentControl>

如果您的代码后台不直接访问XAML元素,则可以使用现有控件执行类似操作(因为UC派生自ContentControl):
<local:MyUserControl>
    <local:MyUserControl.Template>
        <ControlTemplate TargetType="{x:Type local:MyUserControl}">
            <Grid>
                <Border FancyPantsStyling="True">
                    <ContentPresenter/>
                </Border>
            </Grid>
        </ControlTemplate>
    </local:MyUserControl.Template>
</local:MyUserControl>

如果您需要将现有内容与代码后台连接起来,可以使用DataTemplate将外部内容传递(到MyUserControl上的新DP),并将该模板应用于UC的XAML中的ContentControl。


我在同一个问题上纠结了很久,这个答案对我起到了作用。谢谢! - mbmcavoy

2

我有一个想法,尝试了一下,对我有用。我只是想与其他人分享这个想法。希望它有用。

视频链接解释了解决方案的最终结果: 视频链接输入图片描述

基本思路是创建UIElement DependencyProperty而不是创建Border DependencyProperty

首先,您应该向用户控件(在您的情况下为"MyUserControl")添加边框、面板或任何您想要的内容,并确保它有一个名称以从.cs文件访问:

 <Border x:Name="LeftBorder" Grid.Column="0">

然后,您应该在用户控件(在您的情况下是"MyUserControl")中添加一个公共UIElement值:

    public UIElement LeftBorderChild
    {
        get { return (UIElement)GetValue(LeftBorderChildProperty ); }
        set { SetValue(LeftBorderChildProperty , value); }
    }

其次,您的DependencyProperty类型必须是UIElement:
public static readonly DependencyProperty LeftBorderChildProperty = DependencyProperty.Register("LeftBorderChild", typeof(UIElement), typeof(MyUserControl), new PropertyMetadata(new PropertyChangedCallback(LeftBorderChildChanged)));

在这之后,是键盘输入事件:
    public static void LeftBorderChildChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MyUserControl thisUserControl = d as MyUserControl; 
        thisCombobox._LeftBorderChildChanged(e); // Calling local event. The new child will be added in this local event function.
    }
    public void _LeftBorderChildChanged(DependencyPropertyChangedEventArgs e)
    {
        // In this function, new child element will be added to inside of LeftBorder
        this.LeftBorder.Child = (UIElement)e.NewValue; // Sets left border child
    }

我们已经完成了这个类。让我们从其他类中调用它并在其中添加一个控件。
 <local:MyUserControl Width="312" HorizontalAlignment="Right"
                                Margin="48, 0, 0, 0" VerticalAlignment="Center"
                                Height="56"  >
                <local:MyUserControl.LeftBorder>
                    <-- You can insert another control here -->
                    <-- Just don't remember that if you want to add more than one controls, you should add a panel then add controls into inside of the panel because Border child can only 1 child item -->
                    <StackPanel>
                    <-- Now you can insert your controls -->
                    </StackPanel>
                </local:MyUserControl.LeftBorder>
            </local:MyUserControl>

注意:当您首次执行此操作时,您必须在查看xaml设计器之前运行程序。运行程序后,所有设计系统将同步运行。 我希望我理解了您的意思并正确回答了您的问题。 谢谢。

-1

除非我误解了问题,你可以在控件中使用并将其内容设置为任何你需要的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接