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

  • SEARCH
  • Home
  • 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 8421629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:12:03+00:00 2026-06-10T03:12:03+00:00

I m using the binding in Silverlight. I have binded the TextBox with Decimal

  • 0

I m using the binding in Silverlight. I have binded the TextBox with Decimal entity.
Below is the Code snipts of bindings.

<TextBox x:Name="AmountBox" Text="{Binding SelectedEntity.Amount,Mode=TwoWay,StringFormat=\{0:n2\},Converter={StaticResource DecimalBlankValueConverter}}" Validate="True" TextChanged="AmountBox_TextChanged" LostFocus="AmountBox_LostFocus"/>

Below is the converter code.

    decimal result;
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {            
        if (!Decimal.TryParse(value.ToString(),out result) || (decimal)value == decimal.Zero)
            return null;
        return decimal.Parse(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || !Decimal.TryParse(value.ToString(), out result))
            return 0.00;
        return decimal.Parse(value.ToString());
    }

on lost focus I am updating the source
with
GetBindingExpression(TextBox.TextProperty).UpdateSource();

Everything is workig well, But convert is not called on lost focus and when I am entering string in the textbox, than convert is not called and it will not convert the textBox text to blank.

Can anyone please suggest me , what is the probelm in the code.

Thanks in advance.
—-Raj

  • 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-06-10T03:12:04+00:00Added an answer on June 10, 2026 at 3:12 am

    TwoWay mode binding with TextChanged and LostFocus events are definitely not the best approach. Why do you call GetBindingExpression(TextBox.TextProperty).UpdateSource(); in the LostFocus if binding does it by default? If you want to update a binding manually, set UpdateSourceTrigger=Explicit.

    Anyway your converter seems fine, but I think you don’t need it. If I understand right, you want to clear the text if it cannot be cast to decimal. In that case you have a few options, like creating your custom TextBox that allows only digits, or you can check NumericUpDown control from Silverlight Toolkit. Also I’ve found similar question here.

    If you have installed Microsoft Expression Blend (if don’t, you can download BlendSLSDK_en.msi from Microsoft Expression Blend Software Development Kit), add System.Windows.Interactivity dll to your project and create simple Behavior like this:

    public class TextBoxClearTextBehavior : System.Windows.Interactivity.Behavior<System.Windows.Controls.TextBox>
    {
        protected override void OnAttached()
        {
            AssociatedObject.LostFocus += AssociatedObjectLostFocus;
            base.OnAttached();
        }
    
        protected override void OnDetaching()
        {
            AssociatedObject.LostFocus -= AssociatedObjectLostFocus;
            base.OnDetaching();
        }
    
        private void AssociatedObjectLostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            decimal result;
            if (!decimal.TryParse(AssociatedObject.Text, out result))
                AssociatedObject.Text = string.Empty;
        }
    }
    

    and use it like

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    
    ...    
    
    <TextBox Text="{Binding Amount, Mode=TwoWay, StringFormat=\{0:n2\}}">
        <i:Interaction.Behaviors>
            <Behavior:TextBoxClearTextBehavior/>
        </i:Interaction.Behaviors>
    </TextBox>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox (AvailableDevicesListBox), which is populated using data-binding in Silverlight 3. The
I am using Silverlight RIA with EF and I have an entity with e.g.
I have created a lookless control using silverlight 4. This control contains a textbox
How do you Clear/Remove DataBinding in Silverlight? similar to: Remove binding in WPF using
I Have a Service and i interact with it from an activity using Binding.
Below is a sample implementation that uses metro API and data binding (using MVVM)
I'm having troubles using the facebook binding from here ( https://github.com/mono/monotouch-bindings/tree/master/facebook ) and the
I have a self-hosted WCF app using Basic HTTP Binding, no SSL, running in
I have some issues with using databinding in silverlight 4 xaml pages, this is
I have this setup that works perfectly when using http. A silverlight 3 client

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.