DataGrid SelectionChanged MVVM [英] DataGrid SelectionChanged MVVM

查看:85
本文介绍了DataGrid SelectionChanged MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 WPF 和 MVVM 框架.我有一个带有两个 DataGrids 的窗口,我想根据另一个的行选择在一个窗口中加载数据.有没有人有任何建议或例子,我尝试了很多方法,但似乎都没有奏效.

I'm just starting out with WPF and MVVM framework. I have a Window with two DataGrids and I would like to load data in one based on the row selection of the other. Has anyone got any advice or examples ,I've tried numerous ways but none seem to work out.

谢谢

推荐答案

看我可以帮你一点,你可能需要监视所选项目(通过绑定或事件触发器).当它更改为使用新项目从您的数据中获取所需信息,然后重新填充第二个数据网格的源集合时.

Look I can help you a little bit, you maybe need to monitor the selected item (either with binding or an event trigger). When it changes to use the new item to fetch the needed info from your data and then re-populate the source collection for the second data grid.

这是一个可以帮助您的示例代码:

Here is a sample code it can help you:

<DataGrid SelectedValue="{Binding Path=SelectedValue}"
          ItemSource="{Binding Path=Source1}"/>
<DataGrid ItemSource="{Binding Path=Source2}"/>

背后的代码

public ObservableCollection Source1 { get;私人订制;}

Code Behind

public ObservableCollection Source1 { get; private set; }

public ObservableCollection<data> Source2 { get; private set; }

public Data SelectedValue
{
    get { return _selectedValue; }
    set
    {
        if (_selectedValue == value) return;
        _selectedValue = value;
        PopulateSource2();
    }
}

private void PopulateSource2()
{
    Source2.Clear();
    //Get your other data from DB here

    Source2.Add(SelectedValue);//This is just to show that it works
}

这篇关于DataGrid SelectionChanged MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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