2015-05-14 36 views
0

我想创建一个UserControl(如ItemsControl),其中可能包含多个元素。如何创建UserControl,其中可能包含C#/ WPF中的其他元素

我想用这个用户控件这样的:

<local:MyUserControl Margin="10,34,10,10" Background="#FF202020"> 
    <local:OtherUserControl Background="#FF202020" SelectedBackground="#FF303030" /> 
    <local:OtherUserControl Background="#FF202020" SelectedBackground="#FF303030" /> 
    <local:OtherUserControl Background="#FF202020" SelectedBackground="#FF303030" /> 
    <local:OtherUserControl Background="#FF202020" SelectedBackground="#FF303030" /> 
    <local:OtherUserControl Background="#FF202020" SelectedBackground="#FF303030" /> 
</local:MyUserControl> 

我应该在XAML写尽它的工作原理就像我需要什么?你能给我任何代码的例子吗?

回答

1

UserControls(在WPF中)将呈现一个孩子,但该孩子(取决于您使用的控件类型)可以包含子项目。例如,如果您向用户控件添加了一个StackPanel,那么StackPanel可以有多个子项。

在WPF的所有控件将呈现一个孩子,除非控制就像GridStackPanelCanvasDockPanel等布局控制...

下面是一个使用StackPanel另一个例子:

<UserControl x:Class="Drawing.Views.BidForm" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     > 
    <StackPanel> 
    <Label FontWeight="Bold" HorizontalContentAlignment="Center" > Item1</Label> 
    <Label FontWeight="Bold" HorizontalContentAlignment="Center" >Item2</Label> 
    </StackPanel> 
</UserControl> 
+0

我不需要在我的用户noly使用的部件,我需要用我的用户在我的主窗口,在这个窗口中,我需要使用: ' <地方:的MyUserControl> <其它这里元素> ' – SkyDancer

0

你需要从ItemsControl派生你的控件。 UserControl只提供一个内容元素。

或者,您的UserControl可以公开您可以将其他控件绑定到其内容的布局元素。

相关问题