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 1050897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:50:30+00:00 2026-05-16T16:50:30+00:00

This one has me stumped. I found some code posted by Ray Burns to

  • 0

This one has me stumped. I found some code posted by Ray Burns to create a DependencyProperty for a TextBox that allows you to restrict which characters can be deleted by the user. I modified it a little to instead restrict which characters can be inserted, and used that to create TextBoxes that only accept numeric input (plus the decimal point).

This works great for entering text via the keyboard, pasting, dragging and dropping, etc. The only problem comes when setting the Text through code. There it allows non-numeric text to be entered, which in and of itself isn’t really the issue. The issue is that if you check the value of the TextBox’s Text property after doing that, it says it is an empty string.

Here’s some code to demonstrate what I mean. A simple WPF Window:

<Window x:Class="TestApp.Test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:TestApp"
        Title="Test" Height="125" Width="200">
    <Canvas>
        <TextBox x:Name="txtTest" Canvas.Left="10" Canvas.Top="10" Width="100" my:TextBoxRestriction.RestrictInsertTo=".0123456789"></TextBox>
        <Button Canvas.Left="10" Canvas.Top="40" Click="Button_Click">Enter Text</Button>
        <Button Canvas.Left="75" Canvas.Top="40" Click="Button_Click_1">Check Value</Button>
    </Canvas>
</Window>

Its code-behind:

using System;
using System.Windows;

namespace TestApp
{
    public partial class Test : Window
    {
        public Test()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            txtTest.Text = "Test";
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(txtTest.Text, "Length = " + txtTest.Text.Length.ToString());
        }
    }
}

And my modification of Ray’s class:

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace TestApp
{
    //Based on code by Ray Burns at https://stackoverflow.com/questions/3051590/how-to-track-which-character-is-deleted-in-textbox-in-wpf/3056168#3056168.
    public class TextBoxRestriction : DependencyObject
    {
        //RestrictInsertTo: Set this to the characters that may be inserted.
        public static string GetRestrictInsertTo(DependencyObject obj)
        {
            return (string)obj.GetValue(RestrictInsertToProperty);
        }

        public static void SetRestrictInsertTo(DependencyObject obj, string value)
        {
            obj.SetValue(RestrictInsertToProperty, value);
        }

        public static readonly DependencyProperty RestrictInsertToProperty = DependencyProperty.RegisterAttached("RestrictInsertTo", typeof(string), typeof(TextBoxRestriction), new PropertyMetadata
        {
            PropertyChangedCallback = (obj, e) =>
            {
                var box = (TextBox)obj;

                box.TextChanged += (obj2, changeEvent) =>
                {
                    var oldText = GetUnrestrictedText(box);
                    var allowedChars = GetRestrictInsertTo(box);

                    if (box.Text == oldText || allowedChars == null) return;

                    foreach (var change in changeEvent.Changes)
                    {
                        if (change.AddedLength > 0)
                        {
                            string added = box.Text.Substring(change.Offset, change.AddedLength);

                            if (added.Any(ch => !allowedChars.Contains(ch)))
                            {
                                var ss = box.SelectionStart;
                                var sl = box.SelectionLength;

                                box.Text = oldText;
                                box.SelectionStart = ss;
                                box.SelectionLength = sl;
                            }
                        }
                    }

                    SetUnrestrictedText(box, box.Text);
                };
            }
        });

        //UnrestrictedText: Bind or access this property to update the Text property bypassing all restrictions.
        public static string GetUnrestrictedText(DependencyObject obj)
        {
            return (string)obj.GetValue(UnrestrictedTextProperty);
        }

        public static void SetUnrestrictedText(DependencyObject obj, string value)
        {
            obj.SetValue(UnrestrictedTextProperty, value);
        }

        public static readonly DependencyProperty UnrestrictedTextProperty = DependencyProperty.RegisterAttached("UnrestrictedText", typeof(string), typeof(TextBoxRestriction), new PropertyMetadata
        {
            DefaultValue = "",
            PropertyChangedCallback = (obj, e) =>
            {
                var box = (TextBox)obj;

                box.Text = (string)e.NewValue;
            }
        });
    }
}

If you try typing in the TextBox, you’ll see it works pretty much as expected, and that clicking the Check Value button accurately reflects the text; however, if you click the Enter Text button, then click the Check Value button, you’ll see that the app thinks the TextBox’s Text property is an empty string (even though it has text clearly visible in the UI). If you modify the text in the UI in any way (delete a character, say), then click Check Value, it now recognizes the correct text.

Can anyone shed any light on why this is happening? I may be missing something patently obvious, but I just can’t seem to figure it out.

Thanks in advance!

Jeff

  • 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-16T16:50:30+00:00Added an answer on May 16, 2026 at 4:50 pm

    You need to also do an obj.SetValue(TextProperty, value) in your SetUnrestrictedText method, I think.

    That’s a first guess. I’ll look into it a little more deeply if that’s not it and no one else replies.

    I attempted this with your sample code on my machine, VS2010, .NET 4. The attached property resets the TextBox‘s Text property each time. I tried several variants of invalid input.

    If what you want is for the DependencyObject to filter out the assignment-in-code, rejecting invalid characters and keeping valid ones, then you need a different test than if (added.Any(ch => !allowedChars.Contains(ch))).

    • 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.