0

他のコントロール (ボタン) のリストを表示するカスタム コントロールを作成したいと考えています。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 の例

どうもありがとうございました!

4

1 に答える 1

0

これは私の質問に答えました:stackoverflow.com/questions/16958476/…

于 2014-05-24T14:49:00.537 に答える