填充和绑定两个组合框WPF Caliburn.micro [英] Filling and binding two combobox WPF Caliburn.micro
问题描述
我有这个表格:
我在我的项目中使用这个视图调用NewItem,在这个视图中有两个组合框。
I use in my project this view Called NewItem and in this view there is two combobox.
>
我想这样做:
在组合框组中有所有表GROUP的描述,当我选择一个描述的项目(第一个组合框),第二个组合框填充的描述仅涉及我之前选择的描述。
I would like to do this : that in the combobox Group there are all DESCRIPTION of table GROUP, and when i choose an item of this description (of first combobox) the second combobox fills of descriptions relating only to that description that I have chosen before.
这是一些代码:
XAML NewItemView:
XAML NewItemView:
<ComboBox Height="21" HorizontalAlignment="Left" Margin="89,99,0,0"
VerticalAlignment="Top" Width="106" x:Name="Group" SelectedItem="{Binding SelectedGroup}" />
ViewModel代码如下:
The ViewModel code is like:
[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 IS:
GROUP CLASS IS :
public class Group
{
public int Id { get; set; }
public string Descripion { get; set; }
}
我想指定我使用Caliburn.Micro和Dapper for acces'查询。
I wanted to specify that i use Caliburn.Micro and Dapper for acces'query.
非常感谢!
推荐答案
这是一个典型的主/细节场景,有一个典型和简单的解决方法。
This is a typical Master/Detail scenario and there is a typical and easy way to solve it.
I。而不是只在 GetGroups
方法中加载作为 string []
的描述,请加载enitre Group
对象,或者如果有很多属性创建只有两个所需属性的视图模型,则类似如下:
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;}
}
在 NewItemViewModel
中为第二个ComboBox添加属性,假设
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");
}
}
}
现在在您的 NewItemViewModel
中,属性变成这样:
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.
这篇关于填充和绑定两个组合框WPF Caliburn.micro的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!