【问题标题】:WPF custom IList DependencyProperty in custom control behaving oddly自定义控件中的 WPF 自定义 IList DependencyProperty 行为异常
【发布时间】:2014-05-21 13:32:11
【问题描述】:

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

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

Screenshot

我以类似的方式添加了一个自定义“页脚”属性,该属性按预期工作。 我不知道我做错了什么,所以任何帮助将不胜感激。我感觉它与默认值“New List(Of Control)”有关。

示例项目可以在这里找到:CustomControl Example

非常感谢!

【问题讨论】:

标签: wpf vb.net dependency-properties templatebinding


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多