5

我有一个用户控件,我希望能够拥有多个孩子。它已经有了 StackPanel 作为它的孩子,所以我做错了什么?

最终,我希望控件自动包含一些它自己的子元素,然后在使用它时允许在其中一个元素中放置更多的子元素。我尝试了 MSDN 的 How to Override the Logical Tree但没有成功。

提前致谢。

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>

主要.xaml:

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

2 回答 2

2

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

于 2010-02-17T00:00:19.033 回答
-1

Try to add Grid in your UserControl and add rest of child to that grid.

于 2012-09-24T05:21:56.093 回答