UpdateSourceTrigger=PropertyChanged 和转换器
我有一个简单的 Converter
,它将“+”符号添加到 TextBox
中输入的正数。 输入数字时,我想启动一些操作,但我不想等到 TextBox
失去焦点:我想在用户输入文本时立即更新绑定。
TextBox
的默认行为是,当用户离开该框时,绑定源会更新 (UpdateSourceTrigger=LostFocus)。 在这种情况下,我的转换器按预期工作,并且添加了+。 但是,当我将其更改为以下内容时,+ 永远不会添加。
<TextBox Text="{Binding Value, Converter={StaticResource PlusConverter}, UpdateSourceTrigger=PropertyChanged}" />
我可以想象我的 converter.Convert() 方法没有被调用是有充分理由的。 我希望有以下行为:
- 当用户输入文本时,立即更新源
- 当
TextBox
失去焦点时,添加 + 。
现在我正在寻找一种很好的但特别通用的方法来执行此操作(因为我的应用程序中有很多这样的文本框)。 到目前为止我还没能想出一个合适的解决方案。
I have a simple Converter
that adds a "+" symbol to a positive number that is entered in a TextBox
. When the number is entered I want to initiate some action, but I don't want to wait until the TextBox
loses focus: I want to update the binding immediately as the user enters the text.
The default behaviour of a TextBox
is that when a user navigates away from the box, the binding source is updated (UpdateSourceTrigger=LostFocus). In this scenario, my converter works as expected, and the + is added. However, when I change it to the following, the + is never added.
<TextBox Text="{Binding Value, Converter={StaticResource PlusConverter}, UpdateSourceTrigger=PropertyChanged}" />
I can imagine that there is a good reason why my converter.Convert() method is not called. I would like to have the following behaviour:
- When the users enters text, the source is updated immediately
- When the
TextBox
loses focus, the + is added.
Now I'm looking for a nice, but especially GENERIC way of doing this (as I have a lot of these TextBoxes in my app).
So far I haven't been able to come up with a proper solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
同意 Kent B,您需要发布您的转换器代码。
我已经能够让第 1 部分与一个简单的转换器一起使用(我正在绑定第二个未转换的 TextBlock 以显示该值确实正在更新)。
但是,如果我理解您的第 2 部分,您将尝试让 TextBox 的文本在失去焦点后用“+”进行更新。 这有点棘手,我认为仅使用 IConverter 无法做到这一点。 如果可以的话,我也很想知道答案。
您本质上要求的是带水印的输入行为,例如允许用户输入一些数据,并使其格式正确(在底层 DataBinding 和 UI 中)。 最快/最肮脏的解决方案是处理文本框的 LostFocus,但由于您在整个应用程序中都使用它,因此这可能不可行。
您还可以考虑将 TextBox 包装在您自己的 UserControl 中。 如果您查看 WPFToolkit 的 DatePicker 实现,它具有类似的行为:允许用户输入自由格式文本,然后将值自动转换为 DateTime(如果有效)并以本地化格式显示 DateTime。
HTH。
Agree w/Kent B, you need to post your Converter code.
I've been able to get part 1 to work with a simple converter (I'm binding a second unconverted TextBlock to show that the value is indeed getting updated).
However, if I understand your part 2, you're trying to get the TextBox's text to update with a "+" after it loses focus. This is a little trickier and I don't think you'll be able to do it with just an IConverter. If it can be done, I'd love to know the answer as well.
What you're essentially asking for is watermarked input behavior e.g. allow a user to enter some data, and have it get formatted correctly (both in the underlying DataBinding and in the UI). The quickest/dirtiest solution to this is to handle the TextBoxes' LostFocus but since you're using that all over your app, this may not be feasible.
You could also consider wrapping the TextBox in your own UserControl. If you look at WPFToolkit's implementation of a DatePicker it has similar behavior: allow the user to enter free form text, then auto-convert the value to a DateTime (if valid) and show the DateTime in a localized format.
HTH.
您可能想做的另一件事是编辑 TextBox 的模板并将实际的 PART_ContentHost 向右移动一点,然后让 TextBlock 指示 +/- 部分; 即,将 TextBox 的模板从: 更改
为:
然后,将 TextBlock 的内容绑定到文本,但使用写入“+”或“-”的转换器。 这样,用户就无法删除+/-部分,也不用担心解析它; 如果你想做一些事情,比如把负号变成红色或者其他什么,这也会变得更容易。
The other thing you might want to do, is edit the template for TextBox and move the actual PART_ContentHost to the right a bit, then have a TextBlock indicate the +/- part; i.e. change the template of the TextBox from:
into:
Then, bind the TextBlock's content to the text, but with a converter that either writes a '+' or '-'. This way, the user can't delete the +/- part, and you don't have to worry about parsing it; this also makes it easier if you want to do something like make the negative sign red or something.
感谢您的回答! 我自己进一步研究了这个问题,并提出了以下解决方案(我并不完全满意,但它工作正常)
我创建了一个 CustomControl,为 TextBox 添加了功能。
它为 LostFocus 事件提供了一个事件处理程序
当此事件发生时,我的转换器将被调用。
但是,我解决转换器的方式不是很令人满意(我从与我的文本框关联的样式中获取它)。 Style 有一个 Text 属性的 Setter。 从该设置器我可以访问我的转换器。
当然,我也可以制作一个“PlusTextBox”,但我使用不同的转换器,并且我想要一个通用的解决方案。
Thanks for your answers! I looked into this issue myself a bit futher and came up with the following solution (which I'm not entirely satisfied with, but it works fine)
I've created a CustomControl that adds functionality to the TextBox.
It provided an event handler for the LostFocus event
When this event occurs, my converter is called.
However, the way I resolve the Converter is not very satisfying (I take it from the Style that is associated with my TextBox). The Style has a Setter for the Text property. From that setter I can access my Converter.
Of course I could also make a "PlusTextBox", but I use different converters and I wanted a generic solution.
在你的转换器中,你不能只检查键盘的当前焦点吗? 就像是:
In your converter, couldn't you just check the keyboard's current focus? Something like: