具有CurrencyFormat和PropertyChanged触发器的文本框不接受文本权限(&;#39;) [英] TextBox with CurrencyFormat and PropertyChanged trigger doesn't accept text right
本文介绍了具有CurrencyFormat和PropertyChanged触发器的文本框不接受文本权限(&;#39;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
TextBox
绑定到double
类型的窗口的依赖属性(见下文)。每当用户键入TextBox
WHEN时
TextBox
为空,或- 已选择所有文本,
键入的文本被错误接受。例如:如果我在这两种情况下键入‘5’,结果文本是"$5.00",但是插入符号位于‘5’之前,‘$’之后。如果我尝试键入"52.1",得到的结果是"$2.15.00"。
<Window x:Class="WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="154" Width="240" Name="ThisWindow"
Background="{StaticResource {x:Static SystemColors.AppWorkspaceBrushKey}}">
<Grid>
<TextBox Text="{Binding ElementName=ThisWindow,
Path=Amount,
StringFormat={}{0:c},
UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
MinWidth="100" />
</Grid>
</Window>
如果我删除UpdateSourceTrigger属性,它可以正确键入,但不保持货币格式。
有什么想法吗?
推荐答案
这是因为它尝试在每次按字符后应用格式。
作为另一种选择,我通常只设置TextBox
的样式,以便它仅在未被编辑时应用格式
<Style TargetType="{x:Type TextBox}">
<Setter Property="Text" Value="{Binding SomeValue, StringFormat=C}" />
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Text" Value="{Binding SomeValue, UpdateSourceTrigger=PropertyChanged}" />
</Trigger>
</Style.Triggers>
</Style>
这篇关于具有CurrencyFormat和PropertyChanged触发器的文本框不接受文本权限(&;#39;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文