我有一个TextBox
在WPF窗口绑定到类型double
(见下文)的窗口的依赖项属性。每当在TextBox
用户类型时具有CurrencyFormat和PropertyChanged触发器的TextBox不接受文本权
- 的
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实际上只处理文本,当你绑定到文本以外的东西时,它必须做出一些假设(猜测)。 http://stackoverflow.com/questions/9050054/cannot-assign-a-null-value-to-a-nullable-int32-via-binding – Paparazzi 2012-03-28 15:29:16