2017-08-31 197 views
0

控件,如ListBoxItemsSource屬性,它可以綁定到如何安置的ItemsSource和項目

<ListBox x:Name="ListBoxColours" ItemsSource="{Binding Colours}" /> 

它也有一個Items財產然而,它可以被用來在背後

代碼添加項目
ListBoxColours.Items.Add("Red"); 

我創建其必須在ListBox一個CustomControl,我已經在我的控制曝光的ItemSource允許用戶綁定在自己的項目屬性。

<ListBox 
    x:Name="PART_ListBox" 
    ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType=local:TextBoxComboControl}}" 
    SelectionMode="Single" /> 

...

public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
    "ItemsSource", typeof(IEnumerable), typeof(TextBoxComboControl), new PropertyMetadata(default(IEnumerable))); 

public IEnumerable ItemsSource 
{ 
    get { return (IEnumerable) GetValue(ItemsSourceProperty); } 
    set { SetValue(ItemsSourceProperty, value); } 
} 

...

<local:TextBoxComboControl ItemsSource="{Binding Colours}" /> 

我想補充的能力,爲用戶在代碼中添加項目背後還有,雖然,他們櫃面不想使用綁定。我想知道如何與其他人互動。爲了讓他們使用peoprty,我必須將ListBox項目綁定到我控制的兩個屬性中。

+1

從[Items]的註釋部分(https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items(v = vs.110))。aspx)page:*請注意,您使用Items或ItemsSource屬性來指定應該用於生成ItemsControl內容的集合。當ItemsSource屬性被設置時,Items集合被設置爲只讀和固定大小。* – Clemens

+1

其實,我會說你只需要「中繼」這個屬性。 '... Items {get => PART_ListBox.Items; set => PART_ListBox.Items = value; }'... – Fildor

+0

@Fildor請注意,Items是隻讀屬性。 – Clemens

回答

1

ListBox派生自Selector其來自ItemsControl

如果你看看源代碼ItemsControl

你可以看到:

/// <summary> 
///  ItemsSource specifies a collection used to generate the content of 
/// this control. This provides a simple way to use exactly one collection 
/// as the source of content for this control. 
/// </summary> 
/// <remarks> 
///  Any existing contents of the Items collection is replaced when this 
/// property is set. The Items collection will be made ReadOnly and FixedSize. 
///  When ItemsSource is in use, setting this property to null will remove 
/// the collection and restore use to Items (which will be an empty ItemCollection). 
///  When ItemsSource is not in use, the value of this property is null, and 
/// setting it to null has no effect. 
/// </remarks> 
    [Bindable(true), CustomCategory("Content")] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
    public IEnumerable ItemsSource 
    { 
     get { return Items.ItemsSource; } 
     set 
     { 
      if (value == null) 
      { 
       ClearValue(ItemsSourceProperty); 
      } 
      else 
      { 
       SetValue(ItemsSourceProperty, value); 
      } 
     } 
    } 

如果您查看Items屬性類型​​...它可以處於兩種模式之一(ItemsSource模式或「直接」模式)。

/// <summary> 
/// ItemCollection will contain items shaped as strings, objects, xml nodes, 
/// elements, as well as other collections. (It will not promote elements from 
/// contained collections; to "flatten" contained collections, assign a 
/// <seealso cref="System.Windows.Data.CompositeCollection"/> to 
/// the ItemsSource property on the ItemsControl.) 
/// A <seealso cref="System.Windows.Controls.ItemsControl"/> uses the data 
/// in the ItemCollection to generate its content according to its ItemTemplate. 
/// </summary> 
/// <remarks> 
/// When first created, ItemCollection is in an uninitialized state, neither 
/// ItemsSource-mode nor direct-mode. It will hold settings like SortDescriptions and Filter 
/// until the mode is determined, then assign the settings to the active view. 
/// When uninitialized, calls to the list-modifying members will put the 
/// ItemCollection in direct mode, and setting the ItemsSource will put the 
/// ItemCollection in ItemsSource mode. 
/// </remarks> 

有一個內部成員稱爲_isUsingItemsSource其獲取設置/清除跟蹤它是哪種模式 - 這使得各種方法/屬性行爲不同取決於模式。

「項目」經由CollectionView(其被保持在_collectionView構件)訪問 - 這要麼指向一個InnerItemCollectionViewSetItemsSource被稱爲它包裝訪問直接物品,或與CollectionViewSource.GetDefaultCollectionView創建的CollectionView由於「 itemssource「正在設置。

你很有可能從Control派生,所以你需要提供類似的行爲。也許你可以從ItemsControl中得到這種行爲....取決於你的控制當然如果這是合適的。

相關問題