c# - How to bind user control property to other property? -


i have following user control:

the xaml:

<usercontrol x:class="screenrecorder.timepicker"          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" height="27" width="176"> <grid>     <stackpanel orientation="horizontal">         <textbox  width="150" height="25" text="{binding time}" horizontalalignment="left" verticalalignment="top"/>         <stackpanel orientation="vertical">             <button width="25" height="12.5" horizontalalignment="left" verticalalignment="top" click="btnkeyup_clicked">                 <image source="up.png" height="10" width="10" verticalalignment="top"/>             </button>             <button width="25" height="12.5" horizontalalignment="left" verticalalignment="top" click="btnkeydown_clicked">                 <image source="down.png" height="10" width="10" verticalalignment="top"/>             </button>         </stackpanel>     </stackpanel> </grid> 

the code:

 public partial class timepicker : usercontrol, inotifypropertychanged {     public timepicker()     {         initializecomponent();         this.datacontext = this;         //time = m_time;     }      public static dependencyproperty timeproperty = dependencyproperty.register(         "time", typeof(string), typeof(timepicker));      //private string m_time = datetime.now.tostring();     public string time     {         { return (string)getvalue(timeproperty); }         set         {             setvalue(timeproperty, value);             notifypropertychanged("time");         }     }      private void btnkeyup_clicked(object sender, routedeventargs e)     {         datetime curtime = convert.todatetime(time);         curtime += new timespan(0, 0, 1);         time = curtime.tostring();     }      private void btnkeydown_clicked(object sender, routedeventargs e)     {         datetime curtime = convert.todatetime(time);         curtime -= new timespan(0, 0, 1);         time = curtime.tostring();     }      #region inotifypropertychanged     public event propertychangedeventhandler propertychanged;      private void notifypropertychanged(string propertyname = "")     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     }     #endregion } 

and have user control uses user control follow:

        <stackpanel>             <label content="begin record time" horizontalalignment="left" verticalalignment="top" margin="5"/>             <local:timepicker time="{binding startrecordtime}"/>         </stackpanel> 

startrecordtime looks this:

public string startrecordtime     {         { return m_startrecord; }         set         {             m_startrecord = value;             notifypropertychanged("startrecordtime");         }     } 

i want change startrecordtime according time property , vice versa, time property changing.

thank you.

your binding won't work you've set datacontext of timepicker timepicker itself. you'll notice binding error in output window:

bindingexpression path error: 'startrecordtime' property not found on 'object' ''timepicker' (name='')'. bindingexpression:path=startrecordtime; dataitem='timepicker' (name=''); target element 'timepicker' (name=''); target property 'time' (type 'string')

i'd suggest have more 'sane' experience remove datacontext = this timepicker constructor , set grid's datacontext timepicker element name. add name attribute usercontrol element:

<usercontrol x:name="root" ... 

and set datacontext of grid:

<grid datacontext="{binding elementname=root}"> 

this inherited child elements. need change binding twoway, either explicitly:

<local:timepicker time="{binding path=startrecordtime, mode=twoway}" /> 

or setting default in dependencyproperty registration:

public static readonly dependencyproperty timeproperty = dependencyproperty.register(     "time", typeof(string), typeof(timepicker),      new frameworkpropertymetadata(null, frameworkpropertymetadataoptions.bindstwowaybydefault)); 

i'd note there's no reason timepicker implement inotifypropertychanged. suggest remove this.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -