2

Hi Buds,

I have a Nullable int Property that is bound to TextBox which implements INotifyPropertyChanged, during Runtime when I change the value of the TextBox and loose focus. The Setter for the Property ain't hitting. :-(

Any ideas??!

BTW, Yeah BindingMode is set as TwoWay

Arun Selva Kumar
  • 2,593
  • 3
  • 19
  • 30

2 Answers2

1

I'm not sure if this will work in WinRT, but in WPF you need to specify TargetNullValue in binding :

<TextBox Text="{Binding myNullableInt, TargetNullValue={x:Static sys:String.Empty}}"/>

with that, when user delete the text, the property it bound to will be updated to null. Check this SO question for reference.

UPDATE :

Unfortunately, there is no TargetNullValue in WinRT binding. So your available options are whether to go with @TylerD87's approach or to create and use a converter to convert empty string to null value as explained here.

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
0

I would probably consider binding the TextBox to a string property rather than a nullable int as there is probably some sort of binding error there. Then you can always do something like:

int outValue;
int? nullableOutValue;
nullableOutValue = int.TryParse(stringValue, out outValue) ? (int?)outValue : null;

Afterwards to convert your string property to the proper value. You can do this in the setter of the property and provide error checking for illegal values if you need an int to be entered.

TylerD87
  • 1,588
  • 11
  • 20