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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:17:49+00:00 2026-06-10T09:17:49+00:00

I was on to implement a UserControl called PhoneBox that contains a single TextBox

  • 0

I was on to implement a UserControl called PhoneBox that contains a single TextBox, some custom logic, and has a DependencyProperty called PhoneNo. It was to be used in in a two-way binding scenario with a LostFocus for UpdateSourceTrigger. So I wrote the following code –

XAML (UserControl):

<StackPanel>
    <TextBox Name="txtPhone" MinWidth="120" MinHeight="23" LostFocus="txtPhone_LostFocus" GotFocus="txtPhone_GotFocus"/>
</StackPanel>  

Code-Behind (UserControl):

public partial class PhoneBox : UserControl
{
    //Some Code

    static PhoneBox()
    {
        FrameworkPropertyMetadata phoneNoMetadata =
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnPhoneNoChanged),
                                          new CoerceValueCallback(CoercePhoneNoValue));
        PhoneNoProperty = DependencyProperty.Register("PhoneNo", typeof (string), typeof (PhoneBox),
                                                       phoneNoMetadata,
                                                       new ValidateValueCallback(ValidatePhoneNoValue));
    }

    public readonly static DependencyProperty PhoneNoProperty;
    public string PhoneNo
    {
        get { return (string)GetValue(PhoneNoProperty); }
        set { SetValue(PhoneNoProperty, value); }
    }

    private static void OnPhoneNoChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        PhoneBox phoneBox = (PhoneBox)d;
        string newValue = (string)e.NewValue;

        phoneBox.txtPhone.Text = newValue;
    }

    private static object CoercePhoneNoValue(DependencyObject d, object basevalue)
    {
        return basevalue;
    }

    private static bool ValidatePhoneNoValue(object value)
    {
        return true;
    }

    private void txtPhone_LostFocus(object sender, RoutedEventArgs e)
    {
        this.SetValue(PhoneNoProperty, this.txtPhone.Text);
    }

    private void txtPhone_GotFocus(object sender, RoutedEventArgs e)
    {
        if (!String.IsNullOrEmpty(txtPhone.Text))
            this.txtPhone.Text = this.FilterText(txtPhone.Text);
    }

    private string FilterText(string text)
    {
        //Some cutom logic
    }

    //Some more Code
}  

XAML (Consumer):

<pbc:PhoneBox PhoneNo="{Binding Path=User.Phone, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>  

It works. But my question is, the way I used the txtPhone_LostFocus event handler to set proerty value (and in turn update source), is that appropriate? Is there any more appropriate way to do this? I’m a novice in this DependencyProperty thing, so any guiding, suggestion, comments will be thankfully appreciated.

  • 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-10T09:17:50+00:00Added an answer on June 10, 2026 at 9:17 am

    The WPF way to handle this situation is to have a Binding between your UserControl’s DependencyProperty and the TextBox declared in the UserControl’s XAML file. This is also where you would set the LostFocus UpdateSourceTrigger (which you don’t have to, since it is the default behaviour). You’d declare the Binding on the TextBox (i.e. inside the UserControl), so client code using the UserControl would be free to set another binding on the PhoneNo property (from outside of the UserControl). Also, if your CoerceValue callback only returns the base value, better go without it from the start.

    This might be what David meant initially…

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

Sidebar

Related Questions

Scenario: UserControl that has a read-only TextBox and a Button. TextBox.Text is modified and
My UserControl has a TextBox, that is subscribed to OnTextChanged event. However, since a
I implement an app with some list of process IDs. I used Runtime.getRuntime().exec(ps) to
I'm trying to implement the following: I have an Items Manager, that has an
What is the easiest way to implement a UserControl that mimics a hot track
I have a user control which contains a textbox. I have a class called
A breadboard is a tool that is used to implement simple electronic circuits, it
If for example my userControl has a CheckBox called IsGreatCoder and the userControl is
I have an ASP.NET usercontrol that implements the ValidationProperty attribute. This attribute successfully makes
I have a partial view (UserControl) that implements a simple pager in my Asp.Net

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.