我如何创建一个自定义控件,它采用 UIElement
列表并根据某种逻辑呈现它们?
因为它将处理 UIElement
列表,所以添加控件的最佳方式与 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 面板
:
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/