使用组合框选择DataGrid的数据 [英] Using a ComboBox to select data for a DataGrid

查看:73
本文介绍了使用组合框选择DataGrid的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的XAML中,我有一个ComboBox来选择一个父元素。在ComboBox中选择一项时,应该使用属于ComboBox中选定父级的子元素填充DataGrid(我希望这是有道理的。)。

In my XAML I have a ComboBox which selects a parent element. Selection of an item in the ComboBox should populate a DataGrid with children elements which belong to the selected parent from the ComboBox (I hope this makes sense).

<!-- Select here to populate datagrid -->
<ComboBox ItemsSource="{Binding ContactGroups}"
          DisplayMemberPath="Name"
          SelectedValuePath="Id"
          SelectedValue="{Binding ContactGroup}" />

<!-- ComboBox selection loads data to the grid -->
<DataGrid ItemsSource={Binding Contacts}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" />
        <DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" />
    </DataGrid.Columns>
</DataGrid>

我只想使用数据绑定和ViewModel来做到这一点,但老实说我不知道​​如何传递所选的ContactGroup ID我的ViewModel设置如下:

I had wanted to just use databinding and my ViewModel to do this, but I honestly have no idea how to pass the selected ContactGroup Id to my ViewModel which I've set up with the following:

public class ContactsViewModel
{
    private readonly ObservableCollection<Contact> _Contacts
        = new ObservableCollection<Contact>();
    public IEnumerable<Contact> Contacts
    {
        get { return _Contacts; }
    }

    private ObservableCollection<ContactGroup> _ContactGroups 
        = new ObservableCollection<ContactGroup>();
    private IEnumerable<ContactGroup> ContactGroups
    {
        get { return _ContactGroupsViewModel; }
    }

    // Binding for ContactGroups ComboBox
    public ICommand ListContactGroupsCommand
    {
        get
        {
            return new MyDelegateCommand(ListContactGroups);
        }
    }
    private void ListContactGroups()
    {
        using (ApplicationDbContext Context = new ApplicationDbContext())
        {
            var ContactGroups = Context.ContactGroups.Where
            (
                x => x.Deleted == false
            );

            foreach (var c in ContactGroups)
            {
                AddToContactGroups(c);
            }
        }
    }
    private void AddToContactGroups(ContactGroup group)
    {
        if (!_ContactGroups.Contains(group))
            _ContactGroups.Add(group);
    }

    // Handle selection of a ContactGroup.
    public ICommand ListContactsForGroupCommand
    {
        get
        {
            return new MyDelegateCommand((ContactGroupId) =>
                { ListContacts((int)ContactGroupId); });
        }
    }

   private void ListContacts(int contactGroupId)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            var Contacts = db.Contacts.Where
            (
                x => x.ContactGroupId == contactGroupId &&
                x.Deleted == false
            );
            foreach (var c in Contacts)
            {
                AddToContacts(c);
            }
        }
    }
    private void AddToContacts(Contact contact)
    {
        if (!_Contacts.Contains(contact))
            _Contacts.Add(contact);
    }
}

如何执行我的 ComboBox上的ListContactsForGroupCommand

我认为我可以将其挂接到 SelectionChanged 事件中,但是我现在看到了提供参数值的方法。

I figured I can hook it into the SelectionChanged event, but I see now way to provide a parameter value.

我可以从代码隐藏中做到这一点吗?

Can I do this from the code-behind?

推荐答案

我会这样实现:

查看模型

public class ContactsViewModel
{    
    public IEnumerable<Contact> Contacts
    {
        get 
        { 
            return db.Contacts.Where
            (
                x => x.ContactGroupId == ContactGroup.Id &&
                x.Deleted == false
            ); 
        }
    }

    private ContactGroup _contactGroup = new ContactGroup();
    private ContactGroup ContactGroup
    {
        get { return _contactGroup; }
        set
        {
            _contactGroup = value;
            RaisePropertyChangedEvent("ContactGroup");
            RaisePropertyChangedEvent("Contacts");
        }
    }  

    private ObservableCollection<ContactGroup> _ContactGroups 
    = new ObservableCollection<ContactGroup>();
    private IEnumerable<ContactGroup> ContactGroups
    {
        get { return _ContactGroupsViewModel; }
    }  
}

XAML

<!-- Select here to populate datagrid -->
<ComboBox ItemsSource="{Binding ContactGroups}"
          DisplayMemberPath="Name"
          SelectedItem="{Binding ContactGroup}" />

<!-- ComboBox selection loads data to the grid -->
<DataGrid ItemsSource={Binding Contacts}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" />
        <DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" />
    </DataGrid.Columns>
</DataGrid>

这篇关于使用组合框选择DataGrid的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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