10

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>
Palec
  • 12,743
  • 8
  • 69
  • 138
monstr
  • 1,680
  • 1
  • 25
  • 44
  • possible duplicate of [Default values in WPF DataBinding?](http://stackoverflow.com/questions/2238905/default-values-in-wpf-databinding) – pushpraj Jul 07 '14 at 06:19
  • so do you mean that you want to push the value from control's property to the view model and you are not able to set the value because the property is occupied by binding and there is no other option available in binding? if yes then perhaps defaulting the FallbackValue in binding and the default value of the view model property to same value may help you achieve the same. by default the default value for `boolean` and `IsChecked` is `false` so you may not require any change unless you want default to be true. – pushpraj Jul 07 '14 at 06:29
  • In first sentence you described exactly what I mean. And I want set `IsChecked` as `True` - that is the problem. – monstr Jul 07 '14 at 06:39
  • @pushpraj conceptually, `FallbackValue` is not quite fit for my situation, but it is working – monstr Jul 07 '14 at 06:56

1 Answers1

8

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

Ofir
  • 5,049
  • 5
  • 36
  • 61
  • 1
    No, this will not work, I've already seen that post. `TargetNullValue` is used when property of target equals `Null`. But I have `Boolean` property. – monstr Jul 07 '14 at 06:20