c# - ObservableCollection binding on condition -
i have observable collection marketprices , observable collection have bind itemscontrol below .
1) don't want show items in observable collection , want show items user click add , selected pair (gbpjpy, usdgbp..) needs show in items control.
2) if user changed item in comobobox gbpjpy usdgbp , price( datatemplate) of gbpjpy need update usdgbp.
how can achieve both conditions. please note below code doesn't have real-time update in project have relatime price update well, observable collection updates on price changes.
code far
public class pricemodel : inotifypropertychanged { private double _askprice; private double _offerprice; private string _selectedpair; public pricemodel() { pairs = new observablecollection<string> {"gbpusd", "gbpeur", "usdgbp", "gbpjpy"}; } public double askprice { { return _askprice; } set { _askprice = value; onpropertychanged("askprice"); } } public double offerprice { { return _offerprice; } set { _offerprice = value; onpropertychanged("offerprice"); } } public string selectedpair { { return _selectedpair; } set { _selectedpair = value; onpropertychanged(selectedpair); } } public observablecollection<string> pairs { get; set; } public event propertychangedeventhandler propertychanged; [notifypropertychangedinvocator] protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } } public partial class mainwindow : window { public mainwindow() { initializecomponent(); datacontext = this; marketprices = new observablecollection<pricemodel> { new pricemodel {askprice = 1.60345, offerprice = 1.60335, selectedpair = "gbpusd"}, new pricemodel {askprice = 1.71345, offerprice = 1.71335, selectedpair = "gbpeur"}, new pricemodel {askprice = 1.23345, offerprice = 1.23335, selectedpair = "usdgbp"}, new pricemodel {askprice = 1.34345, offerprice = 1.34335, selectedpair = "gbpjpy"} }; } public observablecollection<pricemodel> marketprices { get; set; } }
xaml
<scrollviewer verticalscrollbarvisibility="auto"> <itemscontrol itemssource="{binding marketprices}"> <itemscontrol.itemcontainerstyle> <style> <setter property="frameworkelement.margin" value="5" /> </style> </itemscontrol.itemcontainerstyle> <itemscontrol.itemtemplate> <datatemplate> <wrappanel allowdrop="true" cliptobounds="true"> <grid> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <combobox itemssource="{binding pairs}" selecteditem="{binding selectedpair}" /> <grid grid.row="1"> <grid.columndefinitions> <columndefinition /> <columndefinition /> </grid.columndefinitions> <stackpanel grid.column="0" orientation="horizontal"> <textblock margin="2" text="ask price" /> <textblock margin="2" text="{binding askprice}" /> </stackpanel> <stackpanel grid.column="1" orientation="horizontal"> <textblock margin="2" text="offer price" /> <textblock margin="2" text="{binding offerprice}" /> </stackpanel> </grid> </grid> </wrappanel> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </scrollviewer>
if understand question correctly, want display list of pairs in combobox , details selected pair not pairs?
if that's case, there couple problems code.
pricemodel
you not need collection of available pairs in pricemodel class. also, not need selectedpair property in class, may intention indicate name of pair, update pricemodel to:
public class pricemodel : inotifypropertychanged { private double _askprice; private double _offerprice; private string _pair; public pricemodel(string pair) { _pair = pair; } public string pair { { return _pair; } } public double askprice { { return _askprice; } set { _askprice = value; onpropertychanged("askprice"); } } public double offerprice { { return _offerprice; } set { _offerprice = value; onpropertychanged("offerprice"); } } public event propertychangedeventhandler propertychanged; protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } }
mainwindow.xaml.cs
you have property named marketprices
hold collection of pairs, no property hold selected pair. add property named selectedpair
of type pricemodel
. updated code this:
public partial class mainwindow : window, inotifypropertychanged { private pricemodel _selectedpair; public mainwindow() { initializecomponent(); datacontext = this; marketprices = new observablecollection<pricemodel> { new pricemodel("gbpusd") {askprice = 1.60345, offerprice = 1.60335, }, new pricemodel("gbpeur") {askprice = 1.71345, offerprice = 1.71335, }, new pricemodel("usdgbp") {askprice = 1.23345, offerprice = 1.23335, }, new pricemodel("gbpjpy") {askprice = 1.34345, offerprice = 1.34335, } }; } public observablecollection<pricemodel> marketprices { get; set; } public pricemodel selectedpair { { return _selectedpair; } set { _selectedpair = value; onpropertychanged("selectedpair"); } } public event propertychangedeventhandler propertychanged; protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } }
mainwindow.xaml
you use combobox display list of available pairs , update bindings textboxes refer selectedpair.
update xaml this:
<grid> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <combobox itemssource="{binding pairs}" selecteditem="{binding selectedpair}" /> <grid grid.row="1"> <grid.columndefinitions> <columndefinition /> <columndefinition /> </grid.columndefinitions> <stackpanel grid.column="0" orientation="horizontal"> <textblock margin="2" text="ask price" /> <textblock margin="2" text="{binding selectedpair.askprice}" /> </stackpanel> <stackpanel grid.column="1" orientation="horizontal"> <textblock margin="2" text="offer price" /> <textblock margin="2" text="{binding selectedpair.offerprice}" /> </stackpanel> </grid> </grid>
sample output
Comments
Post a Comment