c# - Binding on DependencyProperty of custom User Control not updating on change -


i'm having difficulties databinding on custom user control (s). created example project highlight problem. i'm new wpf , mvvm well, bear me...

i created simple view uses databinding 2 ways. databinding on built-in control works fine. custom control doesn't... put breakpoint in propertychangedcallback of control. gets hit once on startup, never again. meanwhile, label have bound same value happily counting down.

what missing? example project follows:

the main window:

<window x:class="wpfmvvmapp.mainwindow"         xmlns:local="clr-namespace:wpfmvvmapp"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" height="350" width="525">     <grid>         <grid.datacontext>             <local:countdownviewmodel />         </grid.datacontext>         <label name="custname" content="{binding path=countdown.chargetimeremaining_mins}" height="45" verticalalignment="top"></label>         <local:usercontrol1 minutesremaining="{binding path=countdown.chargetimeremaining_mins}" height="45"></local:usercontrol1>     </grid> </window> 

here's model:

namespace wpfmvvmapp {      public class countdownmodel : inotifypropertychanged     {         private int chargetimeremaining_mins;         public int chargetimeremaining_mins         {                         {                 return chargetimeremaining_mins;             }             set             {                 chargetimeremaining_mins = value;                 onpropertychanged("chargetimeremaining_mins");             }         }          #region inotifypropertychanged members         public event propertychangedeventhandler propertychanged;         private void onpropertychanged(string propertyname)         {             if (propertychanged != null)                 propertychanged(this, new propertychangedeventargs(propertyname));         }         #endregion      } } 

the viewmodel:

namespace wpfmvvmapp {     public class countdownviewmodel     {         public countdownmodel countdown { get; set; }          dispatchertimer timer;         private const int maxmins = 360;          public countdownviewmodel()         {             countdown = new countdownmodel { chargetimeremaining_mins = 60 };              // setup timers             timer = new dispatchertimer();             timer.tick += new eventhandler(this.systemchargetimerservice);             timer.interval = new timespan(0, 0, 1);             timer.start();         }          private void systemchargetimerservice(object sender, eventargs e)         {             //convert minutes remaining             // demo code - todo: remove             this.countdown.chargetimeremaining_mins -= 1;         }     } } 

here's xaml user control:

<usercontrol x:class="wpfmvvmapp.usercontrol1"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"               mc:ignorable="d"               d:designheight="300" d:designwidth="300">     <grid>         <label name="readout"></label>     </grid> </usercontrol> 

and here's code behind user control:

namespace wpfmvvmapp {     public partial class usercontrol1 : usercontrol     {         #region dependency properties         public static readonly dependencyproperty minutesremainingproperty =                     dependencyproperty.register                     (                         "minutesremaining", typeof(int), typeof(usercontrol1),                         new uipropertymetadata(10, new propertychangedcallback(minutesremainchangedcallback))                     );         #endregion          public int minutesremaining         {                         {                 return (int)getvalue(minutesremainingproperty);             }             set             {                 setvalue(minutesremainingproperty, value);             }         }          static void minutesremainchangedcallback(dependencyobject property, dependencypropertychangedeventargs args)         {             usercontrol1 _readout = (usercontrol1)property;             _readout.minutesremaining = (int)args.newvalue;              _readout.readout.content = _readout.minutesremaining;         }          public usercontrol1()         {             initializecomponent();         }     } } 

your change callback breaking binding.

as skeleton: in window have uc.x="{binding a}" , in property change (in uc) have x=b;. breaks binding since in both cases set x.

to rectify, remove change callback , add label:

 content="{binding minutesremaining, relativesource={relativesource findancestor, ancestortype={x:type usercontrol}}}" 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -