简体   繁体   中英

WPF TextBox.Text property OneWay binding bug?

I have a "User Settings" dialog which has Text Boxes to display the settings values in Settings.Default. I have set the DataContext of the Dialog to Settings.Default and set the Text property bindings to OneWay (to avoid instant update of settings in case the dialog is cancelled)

Some of the settings are directory paths which I set using the System.Windows.Forms.FolderBrowserDialog . This simply sets the Text property of the associated TextBox (accepting the Dialog forces the update of the DC). I also have a "Defaults" button which calls

Settings.Default.Reset().

If I edit the contents of a textbox manually and then click Defaults it resets the contents as expected (so I know the binding is OK). However, if I modify the Text property using the FileBrowserDialog and then click default, the Textbox contents remains at what was selected with the FileBrowserDialog even though the Setting.Default setting has reset to the default setting.

Any ideas??

You should use SetcurrentValue to set the Text dependency property value of your textbox.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.dependencyobject.setcurrentvalue?redirectedfrom=MSDN&view=netcore-3.1#System_Windows_DependencyObject_SetCurrentValue_System_Windows_DependencyProperty_System_Object _

The SetCurrentValue method changes the effective value of the property, but existing triggers, data bindings, and styles will continue to work.

Setting the binding to twoway is not the only way to avoid over writing a binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM