我想创建一个显示其他控件(按钮)列表的自定义控件。我有一个名为 Buttons 的 DependencyProperty
Public Property Buttons As IList
Get
Return GetValue(ButtonsProperty)
End Get
Set(ByVal value As IList)
SetValue(ButtonsProperty, value)
End Set
End Property
Public Shared ReadOnly ButtonsProperty As DependencyProperty = _
DependencyProperty.Register("Buttons", _
GetType(IList), GetType(CustomControl1), _
New PropertyMetadata(New List(Of Control)))
并将其绑定在模板中,如下所示:
<ItemsControl
ItemsSource="{TemplateBinding Buttons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
当像这样在 XAML 中使用控件的多个实例时
<StackPanel>
<local:CustomControl1>
<local:CustomControl1.Header>
<Label Content="Header 1"/>
</local:CustomControl1.Header>
<Label Margin="14" Content="Content 1"/>
<local:CustomControl1.Buttons>
<Button Content="Button 1 A"/>
<Button Content="Button 1 B"/>
</local:CustomControl1.Buttons>
</local:CustomControl1>
<local:CustomControl1>
<local:CustomControl1.Header>
<Label Content="Header 2"/>
</local:CustomControl1.Header>
<Label Margin="14" Content="Content 2"/>
<local:CustomControl1.Buttons>
<Button Content="Button 2 A"/>
<Button Content="Button 2 B"/>
</local:CustomControl1.Buttons>
</local:CustomControl1>
</StackPanel>
所有“按钮”都将分配给控件的最后一个实例,如下图所示:
我以类似的方式添加了一个自定义的“页脚”属性,该属性按预期工作。我不知道我做错了什么,所以任何帮助将不胜感激。我感觉它与默认值“New List(Of Control)”有关。
示例项目可在此处找到:CustomControl 示例
非常感谢!