0
Reply

Cascading comboboxes mvvm linq and entity framework

Mateusz Szyndler

Mateusz Szyndler

Jun 10 2015 4:15 PM
967

Hi All,

I'm trying to create cascading comboboxes on my view. I have a table, imported from sql with ADO.NET Entity Data Model:

public partial class tbl_Activities

{

public string MRU { get; set; }

public string Market { get; set; }

public string Tower { get; set; }

public string FirstActivity { get; set; }

public string SecondActivity { get; set; }

}

First combobox should contain FirstActivity items and the second one SecondActivity items related to the first one.

I'm using method to populate the first one:

public void FillFirstActivity()

{

using (TimerConn context = new TimerConn()) {

dynamic result = (from act in context.tbl_Activitieswhere (act.MRU == MRU() & act.Market == MAR() & act.Tower == TOW())orderby act.FirstActivity).Distinct().ToList();

foreach (void act_loopVariable in result) {

act = act_loopVariable;

_First.Add(act);

}

}

}

Properties:

public ObservableCollection<tbl_Activities> First {

get { return _First; }

set { _First = value; }

}

public ObservableCollection<tbl_Activities> Second {

get { return _Second; }

set { _Second = value; }

}

public tbl_Activities SelectedFirst {

get { return _selectedFirst; }

set {

_selectedFirst = value;

this.Second.Clear();

this.Second.Add(_selectedFirst);

}

}

XAML:

<ComboBox ItemsSource="{Binding EmployeeViewM.First}" SelectedIndex="{Binding EmployeeViewM.SelectedFirst}" DisplayMemberPath="FirstActivity"/>

<ComboBox ItemsSource="{Binding EmployeeViewM.Second}" DisplayMemberPath="SecondActivity"/>

PROBLEM:

I'm not able to populate it correctly. In the first combobox I have doubles (Test1, Test2, Test2), and in the second combobox I get only item related to the item chosen from the first combobox:


First one should be distinct and the second one with all items for the current activity.

First Combobox = 'Test1', 'Test2'

Second Combobox = If 'Test1' then 'Test1Add', If 'Test2' then 'Test2Add', 'Test2Add1'

I don't know what I'm doing wrong here. Maybe I should split it into Two tables with PK - FK and then try? If so, how can I bind it to the comboboxes?

My Table view:

Thank you very much for any suggestions