c# - Wpf DatagridComboBoxColumn binding for two tables? -
i have classes books , publishing houses:
public partial class books { public books() { this.authors = new observablecollection<authors>(); } public int bid { get; set; } public string title { get; set; } public int phid { get; set; } public nullable<short> pubyear { get; set; } public nullable<short> edition { get; set; } public virtual publishinghouse publishinghouse { get; set; } public virtual observablecollection<authors> authors { get; set; } } public partial class publishinghouse { public publishinghouse() { this.books = new observablecollection<books>(); } public int phid { get; set; } public string title { get; set; } public string city { get; set; } public virtual observablecollection<books> books { get; set; } }
so have datagrid shows me of books:
<window.resources> <collectionviewsource x:key="booksviewsource" d:designsource="{d:designinstance {x:type local:books}, createlist=true}"/> <collectionviewsource x:key="booksauthorsviewsource" source="{binding authors, source={staticresource booksviewsource}}"/> <collectionviewsource x:key="authorsviewsource" d:designsource="{d:designinstance {x:type local:authors}, createlist=true}"/> <collectionviewsource x:key="publishinghouseviewsource" d:designsource="{d:designinstance {x:type local:publishinghouse}, createlist=true}"/> </window.resources> <grid datacontext="{staticresource booksviewsource}" margin="0,0,2,0"> <datagrid itemssource="{binding}"> <datagrid.columns> <datagridtextcolumn header="b id" binding="{binding bid}"/> <datagridtextcolumn header="title" binding="{binding title}"/> <datagridtextcolumn header="#" binding="{binding edition}"/> <datagridtextcolumn header="year" binding="{binding pubyear}"/> <datagridcomboboxcolumn x:name="phidcomboboxcolumn" selecteditembinding="{binding phid}" /> </datagrid.columns> </datagrid>
the thing want, set default value of phidcomboboxcolumn
taken book, when want edit it, want see list of phid
's of avaliable publishinghouses
. must somehow bind 'datagridcomboboxcolumn' 2 datasources?
your book
class should have property
containing list of phid's
of avaliable publishinghouses
this:
public observablecollection<int> phidlist {get; set;}
then datagridcomboboxcolumn
changes this:
<datagridcomboboxcolumn header="phids" selecteditembinding="{binding phid}"> <datagridcomboboxcolumn.elementstyle> <style targettype="combobox"> <setter property="itemssource" value="{binding phidlist}"/> <setter property="isreadonly" value="true"/> </style> </datagridcomboboxcolumn.elementstyle> <datagridcomboboxcolumn.editingelementstyle> <style targettype="combobox"> <setter property="itemssource" value="{binding phidlist}"/> </style> </datagridcomboboxcolumn.editingelementstyle> </datagridcomboboxcolumn>
edit
if so, every object of book contain identical collections of avaliable publishinghouses. that's unefficient memory. other solution?
to prevent data redundancy can define phidlist
in parent class (e.g. book-list container view-model if you're using mvvm) , combobox
data this:
<datagridcomboboxcolumn selecteditembinding="{binding phid}" displaymemberpath="" > <datagridcomboboxcolumn.elementstyle> <style targettype="{x:type combobox}"> <setter property="itemssource" value="{binding path=datacontext.bookscontainervm.phidlist, relativesource={relativesource ancestortype={x:type window}}}" /> </style> </datagridcomboboxcolumn.elementstyle> <datagridcomboboxcolumn.editingelementstyle> <style targettype="{x:type combobox}"> <setter property="itemssource" value="{binding path=datacontext.bookscontainervm.phidlist, relativesource={relativesource ancestortype={x:type window}}}" /> </style> </datagridcomboboxcolumn.editingelementstyle> </datagridcomboboxcolumn>
Comments
Post a Comment