【问题标题】:Specific text box string format for binding double property绑定双属性的具体文本框字符串格式
【发布时间】:2015-11-23 15:43:12
【问题描述】:

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

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:“StringFormat”转换器无法转换值“0”(类型“Double”);如果可用,将使用后备值。绑定表达式:路径=双属性; DataItem='ViewModel' (HashCode=486165);目标元素是'TextBox'(名称='');目标属性是“文本”(类型“字符串”) FormatException:“System.FormatException:输入字符串的格式不正确。 在 System.Text.StringBuilder.AppendFormat(IFormatProvider 提供程序,字符串格式,对象 [] 参数) 在 System.String.Format(IFormatProvider 提供程序,字符串格式,对象 [] 参数) 在 System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter 转换器,对象值,类型 targetType,对象参数,CultureInfo 文化)'

我尝试更改字符串格式,但没有出现错误,但我认为当您尝试删除文本框的内容时出现“0.0”有点烦人。

我不知道该怎么做才能解决错误,但也可以在没有“0.0”的情况下删除文本框文本。 你能给我一些建议如何处理这种情况吗? 谢谢!

【问题讨论】:

    标签: wpf textbox string-formatting


    【解决方案1】:

    试试这个

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

    你在第二对花括号中忘记了0:

    如果 textbox.Text 为 null 或为空(即,如果你删除它),那么没有“0.0”,你可以试试这个:

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

    如果TextBox.Text = nullTargetNullValue={x:Static System:String.Empty} 会将默认字符串值设置为空。

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

    注意——你可以阅读这篇文章,它可以帮助我解决很多 wpf 问题:wpf-tutorial.com

    【讨论】:

    • 虽然这段代码理论上可以回答这个问题,但最好能解释一下它为什么起作用。
    • 你是对的!我添加了一个链接,我们可以在其中找到好的 wpf 基础信息。
    • 我添加了一些东西来尝试将 Text 设置为空(如果 Text 为空),而不是“0.0”
    【解决方案2】:

    尝试StringFormat=##.##,如果值为 0.0,则 Text 为空

    【讨论】:

    • 这种格式无法让我添加实际值(例如:45.34)。它只接受整数值...
    • 这取决于您的用户界面语言使用的十进制键。 UI 中的默认值为 Language="US-en" 并显示为 "."并且您需要输入密钥“。”如果您设置 ,您将看到并需要输入一个“,”,所有其他格式都将本地化为意大利语。 CurrentUICulture
    猜你喜欢
    • 2021-03-02
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2014-10-21
    • 2018-02-14
    相关资源
    最近更新 更多