Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit "Mode=Twoway" is required for Toggleswitch

Today I update a Windows Store app according to UI design change. One of the changes is replacing a CheckBox with a ToggleSwitch.

So the code is updated from

<CheckBox IsChecked="{Binding BooleanProperty}" ...

to

<ToggleSwitch IsOn="{Binding BooleanProperty"} ... //does not update data source

Then I notice that toggling ToggleSwitch does not update the underlying BooleanProperty, I have to add Mode=TwoWay to make it work.

<Toggleswitch IsOn="{Binding BooleanProperty, Mode=TwoWay"} ... //update data source

From what I learnt in WPF, I don't have to set Mode=TwoWay explicitly on CheckBox's IsChecked property, because it is TwoWay by default.

In general, user-editable control properties, such as those of text boxes and check boxes, default to two-way bindings, whereas most other properties default to one-way bindings.

And I have been thinking ToggleSwitch is just another CheckBox with better touch support, and it is only available on Windows Store and Windows Phone apps.

Why ToggleSwitch.IsOn is not default to TwoWay binding? Are there other differences between a CheckBox and a ToggleSwitch?

like image 879
kennyzx Avatar asked Dec 09 '14 15:12

kennyzx


1 Answers

The issue is that Microsoft changed a bunch of stuff when they created the "Windows 8" version of WPF. Lots of stuff changed, including the default mode for bindings:

The default is OneWay: the source updates the target, but changes to the target value do not update the source.

(MSDN)

Whereas in WPF:

One of the BindingMode values. The default is Default, which returns the default binding mode value of the target dependency property. However, the default value varies for each dependency property. In general, user-editable control properties, such as those of text boxes and check boxes, default to two-way bindings, whereas most other properties default to one-way bindings.

A programmatic way to determine whether a dependency property binds one-way or two-way by default is to get the property metadata of the property using GetMetadata and then check the Boolean value of the BindsTwoWayByDefault property.

(MSDN)

like image 194
BradleyDotNET Avatar answered Nov 04 '22 04:11

BradleyDotNET