16

Model

public int? SizeLength { get; set; }

XAML

<TextBox Text="{Binding [someViewModel].SizeLength, Mode=TwoWay}"></TextBox>

Once user try to backspace or delete the value in this textbox, a message Value '' cannot be converted.

May I know what's wrong with it?

SuicideSheep
  • 5,260
  • 19
  • 64
  • 117

3 Answers3

35

Use:

{Binding TargetNullValue=''}
4

Since the integrated converters from string to single/double/int of WPF are expecting a string to parse they won't default the null value to 0 because you won't always want that behavior, so as it is written in MSDN :

Gets or sets the value that is used in the target when the value of the source is null.

you use this to define your default value for null input:

{Binding TargetNullValue=''}

TargetNullValue msdn

wonea
  • 4,783
  • 17
  • 86
  • 139
elios264
  • 384
  • 4
  • 19
0

You need to add a reference of mscorlib in your XAML, Nullable or ? in your prop and use TargetNullValue with value "" like Elios said.

xmlns:sys="clr-namespace:System;assembly=mscorlib"

public Nullable<int> Prop { get; set; }

{Binding prop, TargetNullValue=''}
André Mendonça
  • 658
  • 8
  • 13