3

如何创建一个自定义控件,该控件采用UIElements 列表并根据某些逻辑呈现它们?

因为它将处理UIElements 的列表,所以添加控件的最佳方法将与 for 相同,即ListBoxor 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 ?

4

2 回答 2

4

你所描述的是一个 WPF Panel

使用 Panel 元素在 Windows Presentation Foundation (WPF) 应用程序中定位和排列子对象。

也就是说,您可以Panel根据您的自定义逻辑对您的孩子进行子类化和安排。

于 2009-07-22T09:58:12.313 回答
0

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

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

就是这样!

于 2009-11-12T14:03:30.993 回答