0
3 개의 컨트롤이 있습니다. ComboBox
입니다. 각 ItemsSource
은 동일한 마스터 제품 목록을 참조합니다. 각 SelectedItem
은 선택한 제품의 다른 목록에 대한 별도의 배열 색인을 참조합니다. 이 모든 것이 훌륭하게 작동하며 예상대로 프로그램이 작동합니다.ListCollectionView를 참조하는 ComboBox.ItemsSource가 ComboBox.SelectedItem 동작을 변경하는 이유는 무엇입니까?
이제는 제품의 마스터 목록 (예 : 특정 문자열 "베리"포함) 만 필터링하려고합니다. 2 일에 ProductsView 결과를 제품에서 ItemsSource 변경 다음 콤보는 참으로 적절하게 선택 될 때마다
로 설정되어 필터링
- 하면 코드의 .
<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <StackPanel> <ComboBox ItemsSource="{Binding Path=ProductsView}" SelectedItem="{Binding Path=SelectedProducts[0]}" /> <ComboBox ItemsSource="{Binding Path=ProductsView}" SelectedItem="{Binding Path=SelectedProducts[1]}" /> <ComboBox ItemsSource="{Binding Path=ProductsView}" SelectedItem="{Binding Path=SelectedProducts[2]}" /> </StackPanel> </Window>
MainWindow.xaml.cs를 : MainWindow.xaml
ItemsSource="{Binding Path=ProductsView}"
사이ItemsSource="{Binding Path=Products}"
를 뒤집기 시도using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Data; namespace WpfApplication2 { public partial class MainWindow : Window { public List<Product> Products { get; set; } public ListCollectionView ProductsView { get; set; } public ObservableCollection<Product> SelectedProducts { get; set; } public MainWindow() { Products = new List<Product>(); Products.Add(new Product { Name = "Apple" }); Products.Add(new Product { Name = "Orange" }); Products.Add(new Product { Name = "Banana" }); Products.Add(new Product { Name = "Pear" }); Products.Add(new Product { Name = "Strawberry" }); Products.Add(new Product { Name = "Raspberry" }); ProductsView = new ListCollectionView(Products); ProductsView.Filter = (x) => (x as Product).Name.Contains("berry"); SelectedProducts = new ObservableCollection<Product>(); SelectedProducts.Add(null); SelectedProducts.Add(null); SelectedProducts.Add(null); InitializeComponent(); DataContext = this; } } public class Product { public string Name { get; set; } public override string ToString() { return Name; } } }