wpfbindingdefault-valueoneway

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>

Solution

  • 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