Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGrid SelectionChanged MVVM [closed]

Tags:

c#

mvvm

wpf

telerik

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.

Thank you

like image 686
user3503644 Avatar asked Jul 30 '26 12:07

user3503644


1 Answers

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:

Xaml

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

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
}
like image 82
Moez Rebai Avatar answered Aug 02 '26 02:08

Moez Rebai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!