7

이 유형을 사용할 때 데이터 바인딩이 어떻게 작동하는지 약간 혼란 스럽습니다.CompositeCollection/CollectionViewSource confusion

내가 CompositeCollection 더 데이터 컨텍스트의 개념과의 바인딩 소스 속성을 설정해야 사용하여 내부 그래서 아무것도가 없기 때문에 다음과 같은

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에서

: 컬렉션이 얼마나이 문제가되지 않도록
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.

내가 그것을 통해 당신을 생각 생각 귀하의 질문에
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).

하려면 Collection DependecyProperty는 IEnumerable 유형에 바인딩 할 수 있습니다 생성 된만큼 생성되고 IEnumerable을 구현합니다.
CVS는 Window에서 DataContext를 상속 한 다음 Items에 바인드합니다.

내용물을 바인딩하려고 시도 했더라도 ContentPesenter에 dataContext가 상속 할 수 있어야하므로 작동하지 않습니다. 바인딩 메커니즘은 데이터 콘텍스트로 설정됩니다. Source, 잊어 버렸습니다. 경로를 설정하려면 이것이 무시 된 이유입니다. 당신이 일을 그냥처럼 설정되어 있는지 확인하기 위해 할 일은 :

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

왜 점점되지 upvotes을? 그것을 upvote! – Tuco