Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 597275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:15:45+00:00 2026-05-13T16:15:45+00:00

I am Binding a TextBox with a property which is of type float. Everything

  • 0

I am Binding a TextBox with a property which is of type float. Everything works fine, I change the value in TextBox and it gets updated in property. The problem occurs when I make the TextBox blank, my property doesn’t get updated, it is still having old value. Now I need to use converter in my binding to update property with default value in case of blank value from TextBox. I want to know Why this behavior? Is there any other solution to this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:15:45+00:00Added an answer on May 13, 2026 at 4:15 pm

    Your property is not updating because it’s not possible to convert empty string to float. There are two ways to solve this.

    First way is to add a property which type is string, bind the TextBox with it and implement changing of float property. Like this:

    public partial class Window1 : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public Window1()
        {
            InitializeComponent();
            // don't use this as DataContext, 
            // it's just an example
            DataContext = this;
        }
    
        private float _FloatProperty;
        public float FloatProperty
        {
            get { return _FloatProperty; }
            set
            {
                _FloatProperty = value;
                OnPropertyCahnged("FloatProperty");
            }
        }
    
        private string _StringProperty;
        public string StringProperty
        {
            get { return _StringProperty; }
            set
            {
                _StringProperty = value;
                float newFloatValue;
    
                // I think you want 0 when TextBox is empty, right?
                FloatProperty = float.TryParse(_StringProperty, out newFloatValue) ? newFloatValue : 0;
    
                OnPropertyCahnged("StringProperty");
            }
        }
    
        protected void OnPropertyCahnged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("StringProperty"));
            }
        }
    }
    

    Second way is to use a converter:

    namespace WpfApplication3
    {
        public partial class Window1 : Window, INotifyPropertyChanged
        {
            public static readonly IValueConverter TextBoxConverter = new FloatConverter();
    
            /* code from previous example without StringProperty */
    
        }
    
        public class FloatConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return value.ToString();
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                float f;
                if (value is string && float.TryParse(value as string, out f))
                {
                    return f;
                }
    
                return 0f;
            }
        }
    }
    

    XAML:

    <Window x:Class="WpfApplication3.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WpfApplication3="clr-namespace:WpfApplication3">
        <Grid>
            <TextBox Text="{Binding FloatProperty,  Converter={x:Static WpfApplication3:Window1.TextBoxConverter}}" />
        </Grid>
    </Window>
    

    There is an article about converters

    I prefer the first way with MVVM pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.