Reputation: 111
I wrote a pretty simple WPF application. I'm trying to bind a double property to text box text using a custom string format. Here is the code for view model and the code for window.
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();
}
}
}
When I run the application it works but I get the following error in console:
System.Windows.Data Error: 6 : 'StringFormat' converter failed to convert value '0' (type 'Double'); fallback value will be used, if available. BindingExpression:Path=DoubleProperty; DataItem='ViewModel' (HashCode=486165); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') FormatException:'System.FormatException: Input string was not in a correct format. at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) at System.String.Format(IFormatProvider provider, String format, Object[] args) at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'
I tried to change the string format and the error did not showed up but I think that is a little annoying to have a '0.0' when you try to delete the content of text box.
I don't know what to do in order to solve the error but also to be able to delete text box text without having that '0.0'. Can you give me some advice how to deal with this situation? Thanks!
Upvotes: 1
Views: 3054
Reputation: 235
try this
StringFormat={}{0:#.##}
you forgot 0:
in your second curly braces couple
Then to not have "0.0" if textbox.Text is null or empty (i.e. if you delete it) you may try this:
Text="{Binding DoubleProperty, TargetNullValue={x:Static System:String.Empty}, StringFormat={}{0:##.##}, UpdateSourceTrigger=PropertyChanged}"
TargetNullValue={x:Static System:String.Empty}
will set the default string value to empty if TextBox.Text = null
.
But you may need to set your DoubleProperty
nullable.
Note -- You may read this, it help me for lots of wpf question: wpf-tutorial.com
Upvotes: 2