Filling and binding two combobox WPF Caliburn.micro

China☆狼群 提交于 2019-12-05 09:16:46
Ibrahim Najjar

This is a typical Master/Detail scenario and there is a typical and easy way to solve it.

I. Instead of only loading descriptions as a string[] inside your GetGroups method, load the enitre Group object or if there is many properties create a view model with only the two needed properties, something like this:

class GroupViewModel {
    public int GroupId {get; set;}
    public string Description {get; set;}
}

II. In NewItemViewModel add a property for the second ComboBox, let's say

class NewItemViewModel {
    private ObservableCollection<SubgroupViewModel> _subgroups;
    public ObservableCollection<SubgroupViewModel> Subgroups
    {
        get {
            if (_subgroups == null)
                _subgroups = new ObservableCollection<SubgroupViewModel>();
            return _subgroups;
        }
        set {
            _subgroups = value;
            NotifyPropertyChanged("Subgroups");
        }
    }
}

III. Now in your NewItemViewModel, the properties become something like this:

class NewItemViewModel {
    public GroupViewModel SelectedGroup
    {
        set {
            var currentlySelected = value;
            // LOAD ALL RELATED Subgroup Descriptions FOR currentlySelected.GroupId;
            Subgroups = // LOADED Subgroup DESCRIPTIONS
        }
    }
    public ObservableCollection<GroupViewModel> Group { get { return Groups; } }
}

I hope you get the idea, this is basic outline of the method. I think you can improve it a bit by leveraging some of Selectors Important Properties and using other techniques for loading the data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!