wpf - What is wrong with this DataGridComboBoxColumn binding syntax? -
i've found many answers question, can't seem syntax right. i've tried every logical combination can think of given explanation here , many others i've found has still eluded me after 3 weeks. need syntax of xaml right.
classes: (renamed simplicity/confidentiality)
usercontrol1 - contains 3 global lists, called streets, houses, , cars
street - contains 2 lists of associated houses , cars, called myhouses , mycars
house - presented in datagrid, 1 column being datagridcomboboxcolumn choose street house associated with. has street property called street declared in keep track of , other calculations in get/set.
car - presented in datagrid, 1 column being datagridcomboboxcolumn choose street car associated with. has street property called street declared in keep track of , other calculations in get/set.
if requested, can refactor code behind match above , post it.
xaml
<datagrid itemssource="{binding streets, mode=twoway}"> <datagrid.columns> <datagridtextcolumn header="street" binding="{binding streetid, mode=twoway}"/> </datagrid.columns> </datagrid> <datagrid itemssource="{binding cars, mode=twoway}"> <datagrid.columns> <datagridtextcolumn binding="{binding carid, mode=twoway}"/> <datagridcomboboxcolumn itemssource="{binding streets, relativesource={relativesource findancestor,ancestortype=usercontrol1}, mode=oneway}" selecteditembinding="{binding street}" selectedvalue="{binding streetid}" selectedvaluepath="streetid" displaymemberpath="streetid"> <datagridcomboboxcolumn.elementstyle> <style targettype="combobox"> <setter property="itemssource" value="{binding streets, relativesource={relativesource findancestor,ancestortype=usercontrol1}, mode=oneway}"/> <setter property="isreadonly" value="true"/> </style> </datagridcomboboxcolumn.elementstyle> <datagridcomboboxcolumn.editingelementstyle> <style targettype="combobox"> <setter property="itemssource" value="{binding streets, relativesource={relativesource findancestor,ancestortype=usercontrol1}, mode=oneway}"/> </style> </datagridcomboboxcolumn.editingelementstyle> </datagridcomboboxcolumn> </datagrid.columns> </datagrid> <datagrid itemssource="{binding houses, mode=twoway}"> <!--copy of cars changed names--> </datagrid>
if understand you're trying correctly, think you're after (based on mockup description):
note of variables have different names due mockup e.g. usercontrol
.
<datagridcomboboxcolumn selecteditembinding="{binding street}" displaymemberpath="streetname"> <datagridcomboboxcolumn.elementstyle> <style targettype="combobox"> <setter property="itemssource" value="{binding streets, relativesource={relativesource findancestor,ancestortype=usercontrol}, mode=oneway}"/> <setter property="isreadonly" value="true"/> </style> </datagridcomboboxcolumn.elementstyle> <datagridcomboboxcolumn.editingelementstyle> <style targettype="combobox"> <setter property="itemssource" value="{binding streets, relativesource={relativesource findancestor,ancestortype=usercontrol}, mode=oneway}"/> </style> </datagridcomboboxcolumn.editingelementstyle> </datagridcomboboxcolumn>
for me, displays cars
in datagrid, relevant columns, , selectable streets (you can adapt houses
in similar way).
as think you've read, datagridcomboboxcolumn
bit odd resolving datacontext
. when you're trying set itemssource
@ top of datagridcomboboxcolumn
, it's causing confusion.
this should work example you've described (which i've modelled cars
, houses
having street
object tracking property, rather id).
if decide want incorporate selectedvalue
, use returned street
id (to store id properties on car
, house
, think you'll have setter in style templates.
edit: mockup used (datacontext set self in xaml). n.b objects won't track each other (as wasn't sure set had).
public partial class usercontrol1 : usercontrol { public list<street> streets { get; set; } public list<house> houses { get; set; } public list<car> cars { get; set; } public usercontrol1() { streets = new list<street>(); houses = new list<house>(); cars = new list<car>(); street streetone = new street() { streetid = 1, name = "street one" }; street streettwo = new street() { streetid = 1, name = "street two" }; car carone = new car() { carid = 1, name = "kitt", mystreet = streetone }; car cartwo = new car() { carid = 2, name = "car 2", mystreet = streettwo }; house houseone = new house() { houseid = 1, name = "house 1", mystreet = streetone }; initializecomponent(); streetone.mycars.add(carone); streetone.myhouses.add(houseone); streettwo.mycars.add(cartwo); cars.add(carone); cars.add(cartwo); houses.add(houseone); streets.add(streetone); streets.add(streettwo); } } public class car { public int carid { get; set; } public string name { get; set; } private street _street; public street mystreet { { return this._street; } set { this._street = value; } } } public class house { public int houseid { get; set; } public string name { get; set; } public street mystreet { get; set; } } public class street { public int streetid { get; set; } public string name { get; set; } public list<house> myhouses { get; set; } public list<car> mycars { get; set; } public street() { myhouses = new list<house>(); mycars = new list<car>(); } }
Comments
Post a Comment