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:
- the code you're posting attempts using
string
object returnedbs.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…andstring
immutable type, object won't ever change anyway).
instead, want bind bindingsource
object source, using text
property path.
- also, doesn't make sense combine
bindingmode.oneway
specific settingsupdatesourcetrigger
. 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
Post a Comment