2010-06-16 101 views
4

我正在为一个名为SearchText的搜索文本编写一个依赖属性的用户控件。它是一个依赖属性,因为我想让控件的使用者使用数据绑定。用户控件包含一个WPF文本框,用户可以在其中输入搜索文本。UserControl与自定义依赖属性的问题

我可以使用数据绑定将用户控件的SearchText依赖项属性与TextBox的Text依赖项属性连接起来,但仅当文本框丢失输入焦点时才会触发此绑定。我想要的是每次更改文本后都要更新SearchText。 所以我添加了一个TextChanged事件处理程序到用户控件,我将SearchText设置为Text的值。

我的问题是,SearchText绑定不起作用,源永远不会更新。我究竟做错了什么?

这里的用户控件代码隐藏的相关部分:

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; 
    } 
    ... 
} 

包含用户控件的实例都有它的DataContext设置为有一个属性也被称为SEARCHTEXT对象的窗口。

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

窗口的数据上下文:

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

不幸的是,当我输入在文本框中此setter不叫。 有什么建议吗?


以下后Quartermeisters暗示我已经删除了TextBox_TextChanged事件处理程序,并安装了一个结合这使TextBox.Text和UserControl.SearchText同步。

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

此绑定似乎工作。但是现在用户控件和窗口的数据上下文之间的绑定被破坏(源不会被更新)。我已经改变了它一点点

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

但没有效果。

有什么特别的我必须注意关于这些“链接”绑定?

回答

4

您可以强制文本框来更新绑定源通过引发LostFocus默认改变UpdateSourceTrigger到的PropertyChanged每次文字的变化:

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

参见Binding.UpdateSourceTrigger MSDN文章。


在您更新的问题上,它看起来像source属性没有更新,因为您正在执行单向绑定。您可以做出的XAML结合双向通过指定Mode

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

或者你可以在你的依赖属性,它是什么呢TextBox.Text指定FrameworkPropertyMetadataOptions.BindsTwoWayByDefault

public static readonly DependencyProperty SearchTextProperty = 
    DependencyProperty.Register("SearchText", typeof(string), typeof(UserControlSearchTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 
+0

谢谢你的提示! 但现在,我现在有两个绑定有问题,请查看我更新的问题。 – 2010-06-16 14:45:01

+0

如果我将模式设置为OneWayToSource,我的两个绑定工作,所以现在所有问题都回答了。谢谢。 – 2010-06-17 06:59:59

+0

之前没有注意到您更新的答案...现在我已经尝试设置FrameworkPropertyMetadataOptions.BindsTwoWayByDefault并将两种绑定都保留下来 - 并且它可以工作!再次感谢。 – 2010-06-17 07:18:06

0

您需要指定UIPropertyMetadata构造函数的PropertyChangedCallback参数。

This CodeProject article does not do what you want。请参阅如何使用Windows Vista Search API从WPF应用程序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) { 
     ... 
    } 
+0

谢谢您答案,但我认为这不是我所需要的。也许你可以看看我更新的问题。 – 2010-06-16 14:52:02

+0

所有问题现在回答。谢谢。 – 2010-06-17 07:00:32