7

これらの型を使用しているときにデータバインディングがどのように動作するか少し混乱します。CompositeCollection/CollectionViewSource confusion

私はCompositeCollectionはDataContextのの概念を持っていないので、バインディングを使用してその中の何かがSourceプロパティを設定する必要があるため、次の

public partial class Window1 : Window 
    { 
     public ObservableCollection<string> Items { get; private set; } 

     public Window1() 
     { 
      Items = new ObservableCollection<string>() { "A", "B", "C" }; 
      DataContext = this; 
      InitializeComponent(); 
     } 
    } 

<Window x:Class="WpfApplication25.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <ComboBox> 
     <ComboBox.ItemsSource> 
      <CompositeCollection> 
       <CollectionContainer Collection="{Binding Items}"/> 
      </CompositeCollection> 
     </ComboBox.ItemsSource> 
    </ComboBox> 
</Window> 

を行うことができないことを読みました。以下のようになります。

<Window x:Class="WpfApplication25.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <Window.Resources> 
     <CollectionViewSource x:Key="list" Source="{Binding Items}"/> 
    </Window.Resources> 

    <ComboBox Name="k"> 
     <ComboBox.ItemsSource> 
      <CompositeCollection> 
       <CollectionContainer Collection="{Binding Source={StaticResource list}}"/> 
      </CompositeCollection> 
     </ComboBox.ItemsSource> 
    </ComboBox> 
</Window> 

しかし、どうしていますか?ソースを何かに設定しますが、何か、この場合はCollectionViewSourceはdatacontext(明示的にソースを設定しない)を使用します。

"list"はWindowのリソースで宣言されているため、Windows DataContextを取得することはできますか?その場合、なぜ次のこともできないのですか?それは親からそれを継承傾けるよう

<Window x:Class="WpfApplication25.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <Window.Resources> 
     <Button x:Key="menu" Content="{Binding Items.Count}"/> 
    </Window.Resources> 

    <ComboBox Name="k"> 
     <ComboBox.ItemsSource> 
      <CompositeCollection> 
       <ContentPresenter Content="{Binding Source={StaticResource menu}}"/> 
      </CompositeCollection> 
     </ComboBox.ItemsSource> 
    </ComboBox> 
</Window> 

答えて

4

あなたがCompositeCollection正しいはdatacontextの概念はありません。 MSDNから

:コレクションがいかに重要ではありませんので、あなたの質問
But how is that working? it sets the source to something, but that something, in this case a CollectionViewSource uses a DataContext (as its not explicitly setting a source).

から
CompositeCollection can contain items such as strings, objects, XML nodes, elements, as well as other collections. An ItemsControl uses the data in the CompositeCollection to generate its content according to its ItemTemplate. For more information about using ItemsControl objects to bind to collections, see the Binding to Collections section of the Data Binding Overview.

私はあなたがオーバーそれを考えると思い、Collection DependecyPropertyはどのIEnumerableタイプに特異的に結合することができます生成され、IEnumerableを実装する限り作成されます。
あなたの場合、CVSはWindowからDataContextを継承し、Itemsにバインドします。

あなたの2番目の例では、ContentPesenterがそれを継承することができるのでdataContextが必要なので動作しません。コンテンツをバインドしようとしたのにバインディングメカニズムがdataContextとして設定されました。パスを設定するには、これが無視された理由だと思います。 あなたはそれを動作させるためにしなければならないすべては、ちょうどそのように設定されている:

<ContentPresenter Content="{Binding Source={StaticResource menu}, Path=Content}"/ 
+1

なぜこれがupvotesを取得していませんか?それをupvote! – Tuco