在TextBox上使用StringFormat绑定到double

时间:2021-08-14 16:05:51

I am using WPF's TextBox with a binding on the Text property to a double on my ViewModel.

我正在使用WPF的TextBox,将Text属性上的绑定设置为ViewModel上的double。

My XAML looks like this:

我的XAML看起来像这样:

<TextBox Text="{Binding Path=MyDoubleValue, StringFormat=N2, UpdateSourceTrigger=PropertyChanged}" />

Unfortunately when I switch UpdateSourceTrigger to PropertyChanged and type value 12345, I get 12,354.00 (EDIT: notice the 5 before the 4). This is a result of keeping cursor in the same place after adding , between 2 and 3 by the .NET formatter.

不幸的是,当我将UpdateSourceTrigger切换到PropertyChanged并输入值12345时,我得到12,354.00(编辑:注意4之前的5)。这是在.NET格式化程序添加2到3之后将光标保持在同一位置的结果。

How can I use StringFormat with UpdateSourceTrigger set to PropertyChanged?

如何在将UpdateSourceTrigger设置为PropertyChanged的情况下使用StringFormat?

Note: This is only happening in .NET 4.

注意:这只发生在.NET 4中。

1 个解决方案

#1


8  

Usually you don't want UpdateSourceTrigger to be PropertyChanged on a TextBox.Text binding because this triggers the Validation and Change notification every time a key is pressed.

通常,您不希望UpdateSourceTrigger在TextBox.Text绑定上成为PropertyChanged,因为每次按下键时都会触发验证和更改通知。

If you are doing this only so that if the user hits Enter it will save the value before processing the save command, then I'd suggest hooking into the PreviewKeyDown event and manually updating the source if the key pressed was Enter (Usually I make this an AttachedProperty)

如果你这样做只是为了如果用户点击Enter它将在处理save命令之前保存该值,然后我建议挂钩到PreviewKeyDown事件并手动更新源,如果按下的键是Enter(通常我做这个附属物)

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        var obj = sender as UIElement;
        BindingExpression textBinding = BindingOperations.GetBindingExpression(
            obj, TextBox.TextProperty);

        if (textBinding != null)
            textBinding.UpdateSource();
    }
}

But with that being said, if you still wanted to use UpdateSourceTrigger=PropertyChanged, then consider using the formatting when displaying the value, but remove it while the user is editing it.

但话虽如此,如果您仍想使用UpdateSourceTrigger = PropertyChanged,则在显示值时考虑使用格式,但在用户编辑时将其删除。

<TextBox>
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding Path=MyDoubleValue, StringFormat=N2}" />
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Text" Value="{Binding Path=MyDoubleValue, UpdateSourceTrigger=PropertyChanged}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

#1


8  

Usually you don't want UpdateSourceTrigger to be PropertyChanged on a TextBox.Text binding because this triggers the Validation and Change notification every time a key is pressed.

通常,您不希望UpdateSourceTrigger在TextBox.Text绑定上成为PropertyChanged,因为每次按下键时都会触发验证和更改通知。

If you are doing this only so that if the user hits Enter it will save the value before processing the save command, then I'd suggest hooking into the PreviewKeyDown event and manually updating the source if the key pressed was Enter (Usually I make this an AttachedProperty)

如果你这样做只是为了如果用户点击Enter它将在处理save命令之前保存该值,然后我建议挂钩到PreviewKeyDown事件并手动更新源,如果按下的键是Enter(通常我做这个附属物)

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        var obj = sender as UIElement;
        BindingExpression textBinding = BindingOperations.GetBindingExpression(
            obj, TextBox.TextProperty);

        if (textBinding != null)
            textBinding.UpdateSource();
    }
}

But with that being said, if you still wanted to use UpdateSourceTrigger=PropertyChanged, then consider using the formatting when displaying the value, but remove it while the user is editing it.

但话虽如此,如果您仍想使用UpdateSourceTrigger = PropertyChanged,则在显示值时考虑使用格式,但在用户编辑时将其删除。

<TextBox>
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding Path=MyDoubleValue, StringFormat=N2}" />
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Text" Value="{Binding Path=MyDoubleValue, UpdateSourceTrigger=PropertyChanged}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>