2014-05-21 55 views
0

我想創建一個顯示其他控件(按鈕)列表的自定義控件。 我已經叫一個DependencyProperty按鈕自定義控件中的WPF自定義IList 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

我以類似的方式添加了一個自定義「頁腳」屬性,它按照預期工作。 我不知道我做錯了什麼,所以任何幫助將不勝感激。我有一種感覺,它與默認值「新列表(控制)」有關。

項目的例子可以在這裏找到:CustomControl Example

非常感謝您!

+0

這個回答我的問題: http://stackoverflow.com/questions/16958476/cannot- bind-observablecollection-to-list-dp-from-xaml/16958848#16958848 將按鈕嵌入到ArrayList中,按預期方式工作 – themightylc

回答

0

這個回答我的問題:stackoverflow.com/questions/16958476/...嵌入按鈕裏面一個ArrayList按預期工作