Use:
{Binding TargetNullValue=''}
質問
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?
解決
Use:
{Binding TargetNullValue=''}
他のヒント
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=''}
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=''}