I have this field and property
private long? _defautID;
public long? DefautID
{
get => _defautID;
set
{
if (_defautID != value)
{
_defautID = value;
OnPropertyChanged();
}
}
}
linked to a textbox
<TextBox Content="{Binding
Path=Expertise.DefautID, UpdateSourceTrigger=PropertyChanged}"/>
When I add or remove a digit from the textbox, my property is updated and PropertyChanged is triggered without any problem.
But when I remove the last digit and my textbox becomes empty, my property is not updated (it keeps the last known value) and thus PropertyChanged is not triggered.
Is there any reason for this behavior ? And is there a workaround ?
PropertyChanged implementation :
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Set the TargetNullValue
property of the binding:
<TextBox Text="{Binding
Path=Expertise.DefautID, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}"/>
This is required for the emtpy string in the TextBox
to be successfully converted to default(long?)
and for your property to be set.