用于绑定double属性的特定文本框字符串格式

时间:2015-08-28 21:44:09

标签: wpf textbox string-formatting

我写了一个非常简单的WPF应用程序。 我尝试使用自定义字符串格式将double属性绑定到文本框文本。以下是视图模型的代码和窗口的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace StringFormat
{
    internal class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            DoubleProperty = 0;
        }
        private double _doubleProperty;
        public double DoubleProperty
        {
            get { return _doubleProperty; }
            set
            {
                _doubleProperty = value;
                NotifyPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}


<Window x:Class="StringFormat.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converters="clr-namespace:StringFormat"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <TextBox Grid.Row="0"
                 Grid.Column="0"
                 Width="120"
                 Height="25"
                 TextAlignment="Center"
                 Text="{Binding DoubleProperty, StringFormat={}{##.##},  UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>


namespace StringFormat
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }
}

当我运行应用程序时,它可以工作,但我在控制台中收到以下错误:

System.Windows.Data错误:6:&#39; StringFormat&#39;转换器无法转换值&#39; 0&#39; (键入&#39; Double&#39;);如果可用,将使用后备值。 BindingExpression:路径= DoubleProperty;的DataItem =&#39;视图模型&#39; (的HashCode = 486165);目标元素是&#39; TextBox&#39; (名称=&#39;&#39);目标属性是&#39; Text&#39; (type&#39; String&#39;)FormatException:&#39; System.FormatException:输入字符串的格式不正确。    在System.Text.StringBuilder.AppendFormat(IFormatProvider提供程序,String格式,Object [] args)    在System.String.Format(IFormatProvider提供程序,String格式,Object [] args)    在System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter转换器,对象值,类型targetType,对象参数,CultureInfo文化)&#39;

我试图更改字符串格式并且错误没有显示但我觉得有点烦人的是有一个&#39; 0.0&#39;当您尝试删除文本框的内容时。

我不知道该怎么做以解决错误,但也能够删除文本框文本而不必使用&#39; 0.0&#39;。 你能给我一些如何应对这种情况的建议吗? 谢谢!

2 个答案:

答案 0 :(得分:1)

试试这个

StringFormat={}{0:#.##}

编辑:抱歉没有解释。你可能会读到这个,它帮助了我很多wpf问题。 wpf-tutorial.com 你忘记了0:在你的第二个花括号夫妇

然后没有&#34; 0.0&#34;如果textbox.Text为null或为空(即如果你删除它),你可以试试这个:

Text="{Binding DoubleProperty,TargetNullValue={x:Static System:String.Empty}, StringFormat={}{0:##.##},  UpdateSourceTrigger=PropertyChanged}"/>
  

TargetNullValue = {x:Static System:String.Empty}如果TextBox.Text = null,则将默认字符串值设置为空

但您可能需要将DoubleProperty设置为可空。

答案 1 :(得分:0)

尝试... editText.setSelection(editText.getText().length()); ,如果值为0.0

,则文本为空
相关问题