自定义控件中的WPF自定义IList DependencyProperty行为异常

自定义控件中的WPF自定义IList DependencyProperty行为异常,wpf,vb.net,dependency-properties,templatebinding,Wpf,Vb.net,Dependency Properties,Templatebinding,我想创建一个自定义控件,显示其他控件(按钮)的列表。 我有一个叫做Buttons的DependencyProperty Public Property Buttons As IList Get Return GetValue(ButtonsProperty) End Get Set(ByVal value As IList) SetValue(ButtonsProperty, value) End Set End Prop

我想创建一个自定义控件,显示其他控件(按钮)的列表。 我有一个叫做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>

所有“按钮”将分配给控件的最后一个实例,如下图所示:

我已经以类似的方式添加了一个自定义的“Footer”属性,该属性正在按预期工作。 我不知道我做错了什么,所以任何帮助都将不胜感激。我觉得这与默认值“newlist(Of Control)”有关

示例项目可在此处找到:


多谢各位

这回答了我的问题:stackoverflow.com/questions/16958476/…在ArrayList中嵌入按钮按预期工作

这回答了我的问题:在ArrayList中嵌入按钮按预期工作