【问题标题】:Adding UIElements to custom control (WPF)将 UIElements 添加到自定义控件 (WPF)
【发布时间】:2010-11-12 22:31:33
【问题描述】:

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

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

【问题讨论】:

    标签: wpf wpf-controls uielement


    【解决方案1】:

    您描述的是 WPF Panel

    使用 Panel 元素来定位和 在 Windows 中排列子对象 演示基金会 (WPF) 应用程序。

    也就是说,您可以继承 Panel 并根据您的自定义逻辑安排您的孩子。

    【讨论】:

      【解决方案2】:

      如果您想将用户控件添加到Panel,只需使用面板的Children 属性。 示例:

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

      就是这样!

      【讨论】:

        猜你喜欢
        • 2011-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-14
        • 1970-01-01
        相关资源
        最近更新 更多