HandyControl icon indicating copy to clipboard operation
HandyControl copied to clipboard

Clear btn in textbox breaks binding to binded text property

Open igeeksng opened this issue 7 years ago • 5 comments

Describe the bug While using the textbox, i enabled ShowClearButton. When the clear btn is pressed, it clears the text in the view(Good) but the binded text property still retain it previous value and won't update on any further text changed. Seems like the binding to the ViewModel property is broken.

<hc:TextBox Margin="2,5" hc:InfoElement.ContentHeight="40" hc:InfoElement.Necessary="True" hc:InfoElement.Placeholder="Account Code" hc:InfoElement.Title="Account Code" CharacterCasing="Upper" FontWeight="Regular" ShowClearButton="True" Text="{Binding AccountCode, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" TextWrapping="Wrap" />

Link to video https://cloud.igeeksng.com/index.php/s/gBHmgR9fowKyKgb

igeeksng avatar Jul 24 '19 15:07 igeeksng

I found this article online.

When you set the Text property in the code behind, you are changing the local value of the Text property. The binding in XAML also set local value for the Text property. Actually, the binindg is not disconnected, it is true that the binding value does not affects at that moment. Based on my experience, you could react the binding by invoking the UpdateTarget method on the BindingExpression class explicitly to update the binding value.

igeeksng avatar Jul 24 '19 20:07 igeeksng

After some digging, i realize the binding expression after one click on the cancel btn returns null value.

image

igeeksng avatar Jul 24 '19 21:07 igeeksng

Hello. I finding found a fix to the bug. In the source code of TextBox.

image

I changes ClearValue to SetCurrentValue

igeeksng avatar Jul 25 '19 12:07 igeeksng

@ehinolakingsley a fix is pending https://github.com/HandyOrg/HandyControl/pull/135

ghost1372 avatar Jul 25 '19 12:07 ghost1372

@ghost1372 Awesome. One more thing. image I will love the highlighted part to be string == empty rather than checking for null value. I believe when i pass in null value i don't want to see the data validation error but only on empty character. Thanks

igeeksng avatar Jul 25 '19 12:07 igeeksng