Friday 15 August 2014

wpf - Why my databinding is not working? -



wpf - Why my databinding is not working? -

code behind:

public partial class mainwindow : window { observablecollection<calls> items = new observablecollection<calls>(); public int ivrsuccess { get; set; } public mainwindow() { initializecomponent(); ictodolist.itemssource = items; this.datacontext = new mainviewmodel(); } private void start_click(object sender, routedeventargs e) { // producer-consumer process add together items. summary s = new summary(); s.ivrsuccess = ivrsuccess;

for xaml,

<dockpanel height="280"> <border cornerradius="6" borderbrush="gray" background="lightgray" borderthickness="2" > <scrollviewer verticalscrollbarvisibility="auto"> <itemscontrol height="400" name="ictodolist"> <itemscontrol.itemtemplate> <datatemplate> ... </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </scrollviewer> </border> </dockpanel> <dockpanel margin="20"> <grid name="summarylist"> <grid.columndefinitions> <columndefinition width="*" /> <textbox text="{binding path=ivrsuccess}" grid.column="0" />

what want display ivrsuccess, hence have class:

public class summary : notifyuibase { public int ivrsuccess; public int ivrsuccess { { homecoming ivrsuccess; } set { ivrsuccess = value; raisepropertychanged(); } }

and:

public class notifyuibase : inotifypropertychanged { // minimal implementation of inotifypropertychanged matching msdn // note dependent on .net 4.5+ because of callermembername public event propertychangedeventhandler propertychanged; public void raisepropertychanged([callermembername] string propertyname = "") { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } }

the question doesn't show, why?

you binding mainviewmodel.ivrsuccess, setting new summary().ivrsuccess in button's click method

you need setting value of bound re-create of ivrsuccess property alter on main window.

for example,

public partial class mainwindow : window { mainviewmodel _datacontext; public mainwindow() { initializecomponent(); _datacontext = new mainviewmodel(); this.datacontext = _datacontext; } // technically should bound icommand on mainviewmodel instead private void start_click(object sender, routedeventargs e) { // ui bound _datacontext.ivrsuccess, update whatever _datacontext.ivrsuccess = whatever; } } public class mainviewmodel: notifyuibase { private int ivrsuccess; public int ivrsuccess { { homecoming ivrsuccess; } set { ivrsuccess = value; raisepropertychanged(); } } private observablecollection<calls> items = new observablecollection<calls>(); private observablecollection<calls> items { { homecoming items; } set { items= value; raisepropertychanged(); } } }

in xaml bindings ..

<itemscontrol name="ictodolist" itemssource="{binding items}" ../> ... <textbox text="{binding path=ivrsuccess}"... />

you sound you're new wpf's binding scheme , how datacontext works, if you're interested have post on blog explaining newbies : what "datacontext" speak of.

it may help understand improve how bindings work, , you'll see why current code not working.

wpf xaml data-binding

No comments:

Post a Comment