使用的StringFormat在WPF文本框绑定不工作 [英] WPF textbox binding not working when using stringformat

查看:197
本文介绍了使用的StringFormat在WPF文本框绑定不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题:

我有使用文本框preVIEW,做一些验证上的文字输入的文本框。该文本框绑定到一个双重属性。最初,当用户输入的号码与小数它会崩溃,因为当它的每一个关键笔划后,在更新时的数量为1。它被自动变更为1(舍去小数)和小数点之后加入下一个号码时,它会想插入新字符的位置是3位,但是由于小数点被自动删除不再有一个3位是错误的。 {:D 0} 到XAML我加入的StringFormat = {}解决了这个问题。这个工作得很好,除了事实,当一切加载首次原始值不再显示出来(从数据库中的值)。但是,如果我只删除的StringFormat值将加载但后来我又有了崩溃的问题。

任何想法?

 <文本框Grid.Column =1保证金=0VerticalAlignment =热门
                             控制:TextBoxMask.NumericType =双师型
                             文本={绑定路径= Model.Processor.BulkShiftInMilliseconds,UpdateSourceTrigger =的PropertyChanged,的StringFormat = {} {0:D}}/>
 

解决方案

我找到问题的根源。请访问以下链接:<一href="https://connect.microsoft.com/VisualStudio/feedback/details/588343/changed-behaviour-from-net-3-5-to-net-4-0-of-wpf-textbox-formatting-when-proper" rel="nofollow">https://connect.microsoft.com/VisualStudio/feedback/details/588343/changed-behaviour-from-net-3-5-to-net-4-0-of-wpf-textbox-formatting-when-proper

将被固定在.NET 4.5。

Here is my problem:

I had a textbox that was using textbox preview and doing some validation on the text entered. The textbox is bound to a double property. Originally when a user entered a number with a decimal it would crash because when it is updating after every key stroke when the number was 1. it was automatically changed to 1 (removing the decimal) and when adding the next number after the decimal it would think the position to insert the new character is position 3 but since the decimal was automatically removed there is no longer a position 3 it errors out. I fixed this by adding StringFormat={}{0:d} to the xaml. This worked fine except for the fact that when everything loads for the first time the original value no longer shows up (the value from the database). But if I remove only the StringFormat the value will load but then I have the crashing problem again.

Any ideas?

 <TextBox Grid.Column="1" Margin="0" VerticalAlignment="Top"
                             Controls:TextBoxMask.NumericType="Double"
                             Text="{Binding Path=Model.Processor.BulkShiftInMilliseconds, UpdateSourceTrigger=PropertyChanged, StringFormat={}{0:d}}"/>

解决方案

Found the root of my issue. See the following link: https://connect.microsoft.com/VisualStudio/feedback/details/588343/changed-behaviour-from-net-3-5-to-net-4-0-of-wpf-textbox-formatting-when-proper

Will be fixed in .net 4.5.

这篇关于使用的StringFormat在WPF文本框绑定不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆