Binding a ComboBox to the ObservableCollection Part

I have a WPF application in C #, where I have a class object MyCollectionthat extends ObservableCollection<MyType>, which contains elements in order to bind them to multiple ComboBoxes.

However, each ComboBox must display a subset of this collection (based on a specific property of its elements) and may vary depending on user input.

How can I get this behavior by supporting updating each subset with data from the original collection? Is there any known design pattern for this scenario?


EDIT: Since my wording of this question is easy to understand, here is an example. I have an object ObservableCollection<Person>where the class Personhas properties Ageand Name. I have three combined blocks, the first two should display objects Namefrom Personwith the odd Age, while the third should have those that have even Age. Their roles may change at runtime (for example, the first and last should display odd ages, the second age) If objects are added or deleted to the collection Person, the changes should be reflected on the corresponding ComboBoxes. Nameand Ageproperties can be considered permanent.

+4
source share
1

, - .

ICollectionView , CollectionViewSource, .

Filter, .

MSDN (http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource(v=vs.110).aspx)

:

:

public string Name { get; set; }
public string Capital { get; set; }

public Country(string name, string capital) {
    this.Name = name;
    this.Capital = capital;
}

:

private ObservableCollection<Country> _countries;
private ICollectionView _european;
private ICollectionView _american;

public ObservableCollection<Country> Countries {
    get {
        if (_countries == null) {
            _countries = new ObservableCollection<Country>();
        }

        return _countries;
    }
}

public ICollectionView European {
    get {
        if (_european == null) {
            _european = new CollectionViewSource {
                Source = this.Countries
            }.View;
            _european.Filter += (e) => {
                Country c = e as Country;
                if (c.Name == "UK" || c.Name == "Ireland" || c.Name == "France") {
                    return true;
                }

                return false;
            };
        }

        return _european;
    }
}

public ICollectionView American {
    get {
        if (_american == null) {
            _american = new CollectionViewSource {
                Source = this.Countries
            }.View;
            _american.Filter += (e) => {
                Country c = e as Country;
                if (c.Name == "USA" || c.Name == "Canada" || c.Name == "Mexico") {
                    return true;
                }

                return false;
            };
        }

        return _american;
    }
}

:

private Model _model;

public Model Model {
    get {
        if (_model == null) {
            _model = new Model();
        }

        return _model;
    }
}

public MainWindow() {
    InitializeComponent();
    this.DataContext = this.Model;
    this.Model.Countries.Add(new Country("UK", "London"));
    this.Model.Countries.Add(new Country("Ireland", "Dublin"));
    this.Model.Countries.Add(new Country("France", "Paris"));
    this.Model.Countries.Add(new Country("USA", "Washington D. C."));
    this.Model.Countries.Add(new Country("Mexico", "Mexico City"));
    this.Model.Countries.Add(new Country("Canada", "Ottawa"));
}

XAML:

<StackPanel>
    <ComboBox
        ItemsSource='{Binding Path=European}'>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel
                    Orientation='Horizontal'>
                    <TextBlock
                        Text='{Binding Path=Name}' />
                    <TextBlock
                        Text=', ' />
                    <TextBlock
                        Text='{Binding Path=Capital}' />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <ComboBox
        ItemsSource='{Binding Path=American}'>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel
                    Orientation='Horizontal'>
                    <TextBlock
                        Text='{Binding Path=Name}' />
                    <TextBlock
                        Text=', ' />
                    <TextBlock
                        Text='{Binding Path=Capital}' />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</StackPanel>
+2

Source: https://habr.com/ru/post/1531256/


All Articles