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?
开发者_Go百科
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?
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));
You need to specify the PropertyChangedCallback
parameter for the UIPropertyMetadata
constructor.
This CodeProject article does exactly what you want. See How to Use Windows Vista Search API from a WPF Application at http://www.codeproject.com/KB/WPF/Vista_Search_in_WPF.aspx.
...
new UIPropertyMetadata(
default(string),
new PropertyChangedCallback(TextBox_TextChanged)
)
...
static void TextBox_TextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) {
...
}
精彩评论