将UI元素添加到自定义控件(WPF)

3

如何创建一个自定义控件,该控件接受 UIElement 列表并根据某些逻辑进行渲染?

由于它将处理 UIElement 列表,因此添加控件的最佳方式将与 ListBoxComboBox 相同。

<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的位置?

2个回答

4
你所描述的是WPF面板:
使用面板元素在Windows Presentation Foundation(WPF)应用程序中定位和排列子对象。
也就是说,你可以创建一个自定义的面板类,并根据你的逻辑来排列子元素。

0
如果您想将用户控件添加到一个面板中,只需使用该面板的 Children 属性即可。 示例:
usercontrolObject = new MyUserControl();
panelobj.Children.Add(usercontrolObject);

就是这样!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接