灌装结合2组合框WPF Caliburn.micro [英] Filling and binding two combobox WPF Caliburn.micro

查看:132
本文介绍了灌装结合2组合框WPF Caliburn.micro的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个表:

我在我的项目使用此视图调用的newitem并在该视图中有是二组合框。

I use in my project this view Called NewItem and in this view there is two combobox.

我想这样做:
在下拉列表中集团有表组的所有描述,当我选择(第一组合框),此类别的项目第二只组合框与我以前选择了描述描述罢了。

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.

这是一些code:

XAML NewItemView:

XAML NewItemView:

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

该视图模型code是这样的:

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类是:

GROUP CLASS IS :

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

我想指定我使用Caliburn.Micro和小巧玲珑的acces'query。

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.

我。相反,只加载描述为一个的String [] 你的 GetGroups 方法里面,加载enitre 集团对象,如果有许多属性创建只有两个需要的属性视图模型,是这样的:

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 添加属性为第二组合框,让我们说

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 ,属性成为这样的:

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; } }
}

我希望你的想法,这是该方法的基本轮廓。我想你可以通过利用一些<一个改进了一下href=\"http://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath\">Selectors重要的属性以及使用其他技术的加载数据。

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.

这篇关于灌装结合2组合框WPF Caliburn.micro的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆