Friday, 15 June 2012

c# - How to set a Dependency Property Value? -



c# - How to set a Dependency Property Value? -

please reference code below context.

on start up, text of 2 textboxes "this original value". when testbox's button ("test button") clicked: the text of testbox's textbox alter "set test button" the other textbox's value not change. when window's button clicked, text of both textboxes should alter "set window". however, plain textbox gets updated, testbox not. <-- bug!

it seems way i'm (re)setting test property within testbox obliterates binding.

what proper way of changing dependency property within user command without breaking bindings?

example code:

i've got usercontrol, testbox looks this:

testbox.xaml:

<usercontrol x:class="company.usercontrols.testbox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:name="textboxcontrol"> <stackpanel> <textbox minwidth="100" name="testtextbox" text="{binding path=test, elementname=textboxcontrol, mode=twoway}" /> <button minwidth="100" content="test button" click="buttonbase_onclick" /> </stackpanel> </usercontrol>

testbox.xaml.cs:

using system.windows; namespace company.usercontrols { public partial class testbox { public const string teststring = "set test button"; public testbox() { initializecomponent(); } public static readonly dependencyproperty testproperty = dependencyproperty.register( "test", typeof(string), typeof(testbox), new frameworkpropertymetadata(null, frameworkpropertymetadataoptions.affectsrender)); public string test { { homecoming (string)getvalue(testproperty); } set { setvalue(testproperty, value); } } private void buttonbase_onclick(object sender, routedeventargs e) { /****** obliterates binding ******/ test = teststring; /****** obliterates binding ******/ } } }

and window uses command this:

mainwindow.xaml:

<window x:class="company.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:u="clr-namespace:company.usercontrols" title="mainwindow"> <stackpanel x:name="mystackpanel"> <textbox text="{binding path=mytestvalue, mode=oneway}"/> <u:testbox x:name="mytestbox" test="{binding path=mytestvalue, mode=oneway}"/> <button content="click" click="buttonbase_onclick" /> </stackpanel> </window>

mainwindow.xaml.cs:

using system.windows; namespace company { public partial class mainwindow { public mainwindow() { initializecomponent(); mystackpanel.datacontext = new mything { mytestvalue = "this original value" }; } private void buttonbase_onclick(object sender, routedeventargs e) { mystackpanel.datacontext = new mything { mytestvalue = "set window" }; } } public class mything { public string mytestvalue { get; set; } } }

the problem asking binding scheme out of sync. whole scheme designed maintain bound elements in sync. cases under can set value on dependency property without destroying underlying binding when binding mode set "twoway" or "onewaytosource". under these conditions value transferred source , consequently, scheme kept in sync. however, in case 2 way binding cause both buttons alter both textboxes.

you need utilize 2 dependency properties testbox. first dependency property bound internal text box, , sec bound in parent window. need add together property alter handler sec dependency property (which done in frameworkpropertymetadata). in handler, set value on first dependency property.

since using usercontrol code behind anyways, simpler solution have sec dependency property mentioned above , straight set value (from event handler , property alter handler) onto textbox via x:name.

let me know if need more clarification.

c# wpf xaml data-binding dependency-properties

No comments:

Post a Comment