Filling and snapping two combobox WPF Caliburn.micro

I have this table:

enter image description here

I use this Called NewItem view in my project, and there are two combo boxes in this view.

enter image description here

I would like to do this: that there are all DESCRIPTION GROUP tables in the combobox group, and when I select an element of this description (the first combobox), the second combo box fills the descriptions that apply only to the description that I selected earlier.

This is the code:

XAML NewItemView:

<ComboBox Height="21" HorizontalAlignment="Left" Margin="89,99,0,0" VerticalAlignment="Top" Width="106" x:Name="Group" SelectedItem="{Binding SelectedGroup}" /> 

ViewModel code is similar:

 [Export(typeof(IScreen))] public class NewItemViewModel : Screen { public string SelectedGroup { get; set; } public String[] Group { get { return Groups; } } [..] //Constructor public NewArticleViewModel() { Groups = GetGroups(); } //Method private string[] GetGroups() { OleDbConnection conn = new OleDbConnection(StringConn); List<Group> groups = new List<Group>(); conn.Open(); groups = conn.Query<Group>(Q_SELECT_GROUPS,null,null).ToList(); conn.Close(); string[] array = new string[groups.Count]; for (int i = 0; i < array.Length; i++) { array[i] = groups[i].Descripion; } return array; } } 

GROUP CLASS:

 public class Group { public int Id { get; set; } public string Descripion { get; set; } } 

I wanted to indicate that I am using Caliburn.Micro and Dapper for acces'query.

Thank you very much!

+4
source share
1 answer

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

I am. Instead of loading descriptions as string[] inside your GetGroups method, load the enitre Group object or if there are many properties, create a view model with only two necessary 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, 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 properties will 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 understand that this is the main plan of the method. I think you can improve it a bit by using some Important Selectors properties and using other methods to load data.

+2
source

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


All Articles