Coerce a WPF TextBox not working anymore in .NET 4.0

为君一笑 提交于 2019-11-27 05:36:59

Try using in xaml Explicit instead of PropertyChanged:

<TextBox Text="{Binding Percentage, Mode=TwoWay, UpdateSourceTrigger=Explicit, TargetNullValue={x:Static System:String.Empty}}"
             TextChanged="TextBox_TextChanged" />

and in code behind UpdateSource instead of UpdateTarget

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        // Removed safe casts and null checks
        ((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource();
    }

Tested it and it works. Btw this problem will probably be resolved in a later version of .NET.

You can use PropertyChanged. However, try binding to the EditValueProperty dependency instead of the TextProperty dependency (or events). It will work as desired.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!