c# - How to bind to WPF View with two objects in a Many to Many Relationship? -
given have many many relationship in data access layer (using codefirst - ef) e.g.
public class report{ public int reportid {get;set;} public string reportname {get;set;} public list<reportkeyword> reportkeywords {get;set;} } public class reportkeyword{ public int reportid {get;set;} public int keywordid {get;set;} } public class keyword{ public int keywordid {get;set;} public string keywordname {get;set;} public list<reportkeyword> reportkeywords {get;set;} }
i need create user interface (wpf view) displays listview of reports , each report should display child list view of keywords. therefore can done in viewmodel best way of modelling viewmodel purpose. need create vm necessary properties? reportviewmodel similar properties want display off report object, including collection of keywords.
public class reportviewmodel : viewmodelbase<reportviewmodel> { private string _reportname; public string reportname { { return _reportname; } set { _reportname = value; notifypropertychanged(model => model.reportname); } } private observablecollection<keyword> _keywords; public observablecollection<keyword> keywords { { return _keywords; } set { _keywords = value; notifypropertychanged(model => model.keywords); } } }
i feel bit tedious. how populate collection display on grid? should when report selected, call method sets keywords collection? there better solution scenario?
in personal opinion, seem there already. however, provide view model collection of report
objects, single property of type report
bind selecteditem
property of collection control instead of collection of keyword
objects have already.
with view model, bind reports
property listbox.itemssource
in view , report
property listbox.itemssource
property, users can select various report objects. can bind reportkeywords
property selected report
object listbox
in view:
<listbox itemssource="{binding reports/reportkeywords}" />
this should bind reportkeywords
collection of current, or selected item in other listbox
.
Comments
Post a Comment