Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with UserControl with custom Dependency Property

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?

like image 365
Mathias Koch Avatar asked Jun 16 '10 11:06

Mathias Koch


People also ask

What is custom dependency property in WPF?

Dependency properties are properties that are registered with the WPF property system through Register or RegisterReadOnly calls. The Register method returns a DependencyProperty instance that holds the registered name and characteristics of a dependency property.

Why is dependency property static?

DependencyProperty has to be static (Class level) because when we create multiple objects of the class which has that property and want to refer the default value for that property the value has to come from that static instance of DependencyProperty.

What are the dependency properties?

A dependency property can reference a value through data binding. Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, determination of the final property value is deferred until run time, at which time the value is obtained from a data source.


1 Answers

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));
like image 77
Quartermeister Avatar answered Sep 23 '22 03:09

Quartermeister