将UIElements添加到自定义控件(WPF)

将UIElements添加到自定义控件(WPF),wpf,wpf-controls,uielement,Wpf,Wpf Controls,Uielement,如何创建一个自定义控件,该控件接受UIElements列表,并根据某些逻辑呈现它们 由于它将处理UIElements列表,因此添加控件的最佳方法与for相同,即ListBox或ComboBox <local:SomeControl> <Button Content="First"/> <Label Content="Something other"/> </local:SomeControl> 以下是用户控件的XAML: &l

如何创建一个自定义控件,该控件接受
UIElement
s列表,并根据某些逻辑呈现它们

由于它将处理
UIElement
s列表,因此添加控件的最佳方法与for相同,即
ListBox
ComboBox

<local:SomeControl>
    <Button Content="First"/>
    <Label Content="Something other"/>
</local:SomeControl>

以下是用户控件的XAML:

<UserControl x:Class="_2009_07_22_Wpf_Smooth_Scroller.SomeControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             MinHeight="100" MinWidth="100">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Label Content="Some title"/>

        <!-- The inner UIElement to add content to  -->
        <Canvas x:Name="innerPanel" Grid.Row="1"/>

    </Grid>
</UserControl>


例如,如何将第I个控件放置到位置X=50*I,Y=40*I?

您所描述的是一个WPF
面板

使用面板元素来定位和 在窗口中排列子对象 演示基金会(WPF) 应用程序


也就是说,您可以将
面板
分为子类,并根据自定义逻辑排列子类。

如果要将用户控件添加到
面板
,只需使用面板的
子类
属性即可。 样本:

就这样

usercontrolObject = new MyUserControl();
panelobj.Children.Add(usercontrolObject);