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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:02:34+00:00 2026-05-15T07:02:34+00:00

I’m writing a user control with a dependency property for a search text called

  • 0

I’m writing a user control with a dependency property for a search text called SearchText. It is a dependency property because I want to allow consumers of the control to use data binding. The user control contains a WPF TextBox where the user can enter the search text.

I could use data binding to connect the SearchText dependency property of the user control with the Text dependency property of the TextBox, but this binding only fires when the text box looses input focus. What I want is SearchText to be updated after every change of Text.
So I have added a TextChanged event handler to the user control where I set SearchText to the value of Text.

My Problem is, the SearchText binding doesn’t work, the source never gets updated. What am I doing wrong?

Here’s the relevant part of the user controls code-behind:

public partial class UserControlSearchTextBox : UserControl
{
    public string SearchText
    {
        get { return (string)GetValue(SearchTextProperty); }
        set { SetValue(SearchTextProperty, value); }
    }

    public static readonly DependencyProperty SearchTextProperty =
        DependencyProperty.Register("SearchText", typeof(string), typeof(UserControlSearchTextBox), new UIPropertyMetadata(""));

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        SearchText = ((TextBox)sender).Text;
    }
    ...
}

The window that contains an instance of the user control has its DataContext set to an object that has a property also called SearchText.

<uc:UserControlSearchTextBox SearchText="{Binding SearchText}" />

The data context of the Window:

public class DataSourceUserManual : DataSourceBase
{
    private string _searchText;
    public string SearchText
    {
        get { return _searchText; }
        set
        {
            _searchText = value;
            ...
            OnPropertyChanged("SearchText");
        }
    }
}

Unfortunately, this setter is not called when I type into the text box.
Any ideas?


After following Quartermeisters hint I’ve removed the TextBox_TextChanged event handler and installed a binding which keeps TextBox.Text and UserControl.SearchText in sync.

<TextBox Text="{Binding ElementName=root, 
                        Path=SearchText, 
                        UpdateSourceTrigger=PropertyChanged}" />

This binding seems to work. But now the binding between the user control and the data context of the window is broken (the source is never updated). I’ve changed it a little bit

<uc:UserControlSearchTextBox SearchText="{Binding Source={StaticResource ResourceKey=dataSource}, 
                                                  Path=SearchText}" />

but with no effect.

Anything special I have to take care of regarding these “chained” bindings?

  • 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-15T07:02:35+00:00Added an answer on May 15, 2026 at 7:02 am

    You can force the TextBox to update the binding source every time Text changes by changing the UpdateSourceTrigger to PropertyChanged from the default of LostFocus:

    <TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" />
    

    See the MSDN article on Binding.UpdateSourceTrigger.


    On your updated question, it looks like the source property is not updating because you are doing a one-way binding. You can either make that binding two-way in XAML by specifying the Mode:

    <uc:UserControlSearchTextBox SearchText="{Binding Source={StaticResource ResourceKey=dataSource}, 
                                                  Mode=TwoWay,
                                                  Path=SearchText}" />
    

    Or you can specify FrameworkPropertyMetadataOptions.BindsTwoWayByDefault in your dependency property, which is what TextBox.Text does:

    public static readonly DependencyProperty SearchTextProperty =
        DependencyProperty.Register("SearchText", typeof(string), typeof(UserControlSearchTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.