이 유형을 사용할 때 데이터 바인딩이 어떻게 작동하는지 약간 혼란 스럽습니다.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>
왜 점점되지 upvotes을? 그것을 upvote! – Tuco