以十进制存储十进制数?使用updatesourcetrigger作为PropertyChanged的属性

时间:2016-04-13 10:26:16

标签: c# wpf xaml

我正在使用WPF / MVVM。 我将textbox.Text绑定到视图模型中的可空双精度。 UpdateSourceTrigger = PropertyChanged而不是Lostfocus。因此,当使用我正在使用的转换器内的Double.Parse(textbox.Text)输入每个数字时,将更新double属性。我在这里使用PropertyChanged和转换器,因为我需要做一些其他的验证检查。

我的问题是我需要输入“1.69”。 当我输入“1”时,它被添加为“1”到属性。 接下来我输入“。”,但不会添加为“1”。因为double.parse将数字保存为“1”

所以我无法添加十进制数字。请帮忙。提前谢谢。

3 个答案:

答案 0 :(得分:2)

如果你使用StringFormat=\{0:n\},你应该没问题。例如:

<TextBox Text="{Binding FooValue, UpdateSourceTrigger=PropertyChanged, 
                                               StringFormat=\{0:n\}}"/>

或只是使用转换器。例如:

<Window.Resources>        
   <helper:DoubleConverter x:Key="DoubleConverter" />
</Window.Resources>
...The code omitted for the brevity
<TextBox Text="{Binding Amount, UpdateSourceTrigger=PropertyChanged,       
                        Converter={StaticResource DoubleConverter}}"/>

DoubleConverter

public class DoubleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
       return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
       // return an invalid value in case of the value ends with a point
       return value.ToString().EndsWith(".") ? "." : value;
    }
}

答案 1 :(得分:1)

如果输入的字符串以0结尾,您可以在转换前附加.

答案 2 :(得分:1)

我刚刚在这篇文章的评论中使用转换器做了一个测试应用程序:An issue when TextBox is binding to double and enter negative number that little than -1并且它适用于我。

我的ViewModel:

class MainWindowViewModel
{

    private double? myDouble;
    public double? MyDouble
    {
        get { return myDouble; }
        set
        {
            myDouble = value;
        }
    }
}

我的测试主窗口:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        >
    <Window.Resources>
        <local:DecimalConverter x:Key="converter" />
    </Window.Resources>

    <Grid>
        <TextBox 
            x:Name="textBox" 
            HorizontalAlignment="Left" 
            Text="{Binding MyDouble, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource converter}}"
            Height="23" 
            Margin="10,185,0,0" 
            TextWrapping="Wrap" 
            VerticalAlignment="Top" 
            Width="120"/>

        <TextBox 
            x:Name="textBox1" 
            HorizontalAlignment="Left" 
            Text="{Binding MyDouble}"
            Height="23" 
            Margin="10,248,0,0" 
            TextWrapping="Wrap" 
            VerticalAlignment="Top" 
            Width="120"/>

    </Grid>
</Window>

文本框1按预期从文本框更新。尝试使用Avneesh的转换器。