wpf - 将 UIElement 添加到自定义控件 (WPF)

标签 wpf wpf-controls uielement

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

因为它将处理 UIElement 列表,所以添加控件的最佳方式与 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 面板:

Use Panel elements to position and arrange child objects in Windows Presentation Foundation (WPF) applications.

也就是说,您可以子类化 Panel 并根据您的自定义逻辑安排您的 child 。

关于wpf - 将 UIElement 添加到自定义控件 (WPF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1164056/

相关文章:

c# - 实现捕捉 Canvas 网格线

c# - 为什么克隆的 UIElement 或 FrameworkElement 的样式丢失

c# - 从 UIElement 获取图像被裁剪到显示大小

c# - 如何在类似标签的控件中显示简短的只读 FlowDocument

c# - 使用绑定(bind)触发样式更改

wpf - 如何将自动完成框添加到数据网格?

.net - 是什么导致 Windows 在此 WPF 功能区应用程序中挂起

c# - WPF:在 TabItem header 中绑定(bind)

C# EF Code first - 创建数据库和添加数据

WPF-获取内存中UIElement的大小?