Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF OneWayToSource binding initial value

I have a RadioButton element whose IsChecked property is bound to MyProperty in ViewModel. The Binding has mode OneWayToSource for some reasons, it pushes the value from RadioButton.IsChecked to ViewModel.MyProperty.

RadioButton.IsChecked is initially false, now. I want to set an initial value from ViewModel, which might be even true. I cannot do that because the property is occupied by the binding.

Is there any way to use Binding with that mode and set default value to the bound property in UI? Something like that:

<RadioButton IsChecked="{Binding MyProperty, Mode=OneWayToSource, DefaultVaule=???}">
</RadioButton>
like image 466
monstr Avatar asked Jul 07 '14 06:07

monstr


1 Answers

If I understood you correct, I think this might help:

You can define the default value through the TargetNullValue property. You can define a FallbackValue value in case of error either, for example:

<TextBox Text="{Binding MyProperty, TargetNullValue=0, FallbackValue=10}" />

see here: enter link description here

like image 51
Ofir Avatar answered Nov 07 '22 23:11

Ofir