c# - SetBinding not registering for PropertyChanged event -


in application i'm working on, programmatically create several frameworkelements differing data sources. unfortunately, data binding failing.

i managed distill problem following program:

using system.collections.generic; using system.componentmodel; using system.runtime.compilerservices; using windows.ui.xaml; using windows.ui.xaml.controls; using windows.ui.xaml.data;  namespace testbinding {     public class bindingsource : inotifypropertychanged     {         public event propertychangedeventhandler propertychanged;         protected virtual void onpropertychanged(string propertyname)         {             propertychangedeventhandler handler = propertychanged;             if (handler != null) handler(this, new propertychangedeventargs(propertyname));         }         protected bool setfield<t>(ref t field, t value, [callermembername] string propertyname = null)         {             if (equalitycomparer<t>.default.equals(field, value)) return false;             field = value;             onpropertychanged(propertyname);             return true;         }           private string _text = "initial text";         public string text         {             { return _text; }             set { setfield(ref _text, value); }         }     }      public sealed partial class mainpage : page     {         bindingsource bs = new bindingsource();          public mainpage()         {             this.initializecomponent();              textblock tb = new textblock();             tb.horizontalalignment = horizontalalignment.center;             tb.verticalalignment = verticalalignment.center;             tb.fontsize = 36;              binding bind = new binding() { source = bs.text, mode=bindingmode.oneway, updatesourcetrigger=updatesourcetrigger.propertychanged };             tb.setbinding(textblock.textproperty, bind);              patergrid.children.add(tb);         }          private void clicktext1(object sender, routedeventargs e)         {             bs.text = "first text button clicked!";         }          private void clicktext2(object sender, routedeventargs e)         {             bs.text = "second text button stroked!";         }     } } 

and here's xaml:

<page     x:class="testbinding.mainpage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="using:testbinding"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     mc:ignorable="d">      <grid x:name="patergrid" background="{themeresource applicationpagebackgroundthemebrush}">         <grid.columndefinitions>             <columndefinition width="*"/>             <columndefinition width="*"/>         </grid.columndefinitions>          <stackpanel grid.column="1" horizontalalignment="center" verticalalignment="center">             <button content="text 1" margin="25" horizontalalignment="center" click="clicktext1" />             <button content="text 2" margin="25" horizontalalignment="center" click="clicktext2" />         </stackpanel>     </grid> </page> 

by stepping through code when properties changed, see there no subscribers propertychanged event.

why isn't setbinding registering propertychanged event?

you want instead:

binding bind = new binding() {     source = bs,     path = new propertypath("text"),     mode = bindingmode.twoway,     updatesourcetrigger = updatesourcetrigger.propertychanged }; 

there seem 2 problems here:

  1. the code you're posting attempts using string object returned bs.text property, not observable binding source. binding system flexible enough copy value once, after that, there's no way know object has changed (even if could…and string immutable type, object won't ever change anyway).

instead, want bind bindingsource object source, using text property path.

  1. also, doesn't make sense combine bindingmode.oneway specific settings updatesourcetrigger. when binding mode one-way, source never updated doesn't matter trigger is.

using bindingmode.twoway allow changes in textblock.text property copied source.

alternatively, don't bother setting updatesourcetrigger. textblock isn't user-editable anyway, maybe did intend binding one-way , left extraneous initialization of updatesourcetrigger else. it's hard know question right implementation needs.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -