Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF UserControl在另一个UserControl中_Wpf_User Controls - Fatal编程技术网

WPF UserControl在另一个UserControl中

WPF UserControl在另一个UserControl中,wpf,user-controls,Wpf,User Controls,我希望在XAML中将UserControl设置为另一个UserControl的Content,就像您可以将按钮的Content设置为任意内容一样 假设我的“外部”UserControl如下所示: <MyUserControl> <Grid> <Border FancyPantsStyling="True"> <-- I want to insert other controls here -->

我希望在XAML中将
UserControl
设置为另一个
UserControl
Content
,就像您可以将
按钮的
Content
设置为任意内容一样

假设我的“外部”
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
以将其
内容
呈现在特定位置?

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

您放入UserControl的XAML中的所有内容都是它的内容,因此您不能通过设置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):



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

我有了一个想法,然后尝试了一下,效果很好。我只是想把这个分享给其他人。我希望它会有用

解释解决方案结尾的视频链接:

基本思想是创建UIElement DependencyProperty,而不是创建Border DependencyProperty

首先,您应该将边框或面板或任何您想要的内容添加到您的用户控件中(在您的情况下,它是“MyUserControl”),并确保它具有可从.cs文件访问的名称:

 <Border x:Name="LeftBorder" Grid.Column="0">
第二,必须是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>


我一直在为同一个问题头痛。这个答案对我有用。非常感谢。
    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>