WPF UserControl with multiple Children

半城伤御伤魂 提交于 2019-12-13 13:16:15

问题


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?

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.

Thanks in advance.

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:

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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/2277309/wpf-usercontrol-with-multiple-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!