具有多个子级的WPF UserControl [英] WPF UserControl with multiple Children

查看:78
本文介绍了具有多个子级的WPF UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UserControl,我希望能够有多个孩子。

I have a UserControl that I would like to be able to have more than one child. It already has StackPanel as it's child, so what am I doing wrong?

最终,我希望控件自动包含其自己的一些子项,然后,使用时,允许在其元素之一内放置更多孩子。我尝试 MSDN的如何覆盖逻辑树,但没有成功。

Ultimately, I'd like for the control to include some of its own children automatically and then, when it's used, allow placing more children inside one of its elements. I tried MSDN's How to Override the Logical Tree without success.

谢谢。

LayerPanelItem.xaml:

LayerPanelItem.xaml:

<UserControl x:Class="Controls.LayerPanelItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <StackPanel></StackPanel>
</UserControl>

Main.xaml:

Main.xaml:

<controls:LayerPanelItem>   
    <TextBlock>Test</TextBlock>
    <TextBlock>Test</TextBlock> <!-- Error:  The property 'Content' is set more than once. -->
</controls:LayerPanelItem>


推荐答案

您不能(直接)使用UserControl。相反,您需要从 ItemsControl ,并将您自己的自定义布局逻辑放在子类中。

You can't do this (directly) with a UserControl. Instead, you'll need to derive from ItemsControl, and put your own custom layout logic in the subclass.

这篇关于具有多个子级的WPF UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆