Toggleswitch requires explicit "Mode = Twoway"

Today I am updating the Windows Store app in accordance with the redesign of the user interface. One change is replacing CheckBox with ToggleSwitch .

So, the code is updated with

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

to

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

Then I notice that when switching ToggleSwitch not updated, I must add Mode=TwoWay for it to work.

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

From what I learned in WPF , I do not need to explicitly set the Mode=TwoWay property of the CheckBox IsChecked, because it is TwoWay by default.

In general, user-editable control properties, such as text fields and check boxes, have two-way bindings by default, while most other properties are bound to one-way relationships by default.

And I thought ToggleSwitch is another CheckBox with better touch support, and it is only available in the Windows Store and Windows Phone apps.

Why is ToggleSwitch.IsOn not used to bind TwoWay by default? Are there any other differences between CheckBox and ToggleSwitch?

+6
source share
2 answers

The problem is that Microsoft changed a bunch of things when they created the WPF version of "Windows 8". Many things have been changed, including the default mode for bindings:

By default, OneWay is used: the source updates the target, but changing to the target value does not update the source.

( MSDN )

While in WPF:

One of the values โ€‹โ€‹of BindingMode. The default is the default value, which returns the default binding mode value of the target dependency property. However, the default value is changed for each dependency property. In general, user-editable control properties, such as text fields and checkboxes, default to two-sided bindings, while most other properties default to one-way bindings.

The programmatic way to determine if a dependency property binds a one-sided or two-sided default is to get the property property metadata using GetMetadata, and then check the Boolean value of the BindsTwoWayByDefault property.

( MSDN )

+3
source

In my experience, ToggleSwitch and CheckBox are used to switch between two states. Of course, ToggleSwitch is used in the Windows Store and Windows Phone applications. Perhaps ToggleSwich is more like a simple ToggleButton than a CheckBox ... Another difference is that the CheckBox can be cleared, but ToggleSwitch is always in or without switching mode.

In any case, I suggest setting the binding mode explicitly in each case.

0
source

Source: https://habr.com/ru/post/979336/


All Articles