2012-06-01 4 views
0

Я новичок в WPF и я пытаюсь связать свойство dependacy. Я хочу, что текст я пишу на WPFCtrl: FilterTextBox будет displaied в TextBlockНевозможно привязать WPF DependencyProperty

вот мой XAML

xmlns:WPFCtrl="clr-namespace:WPFControls" 
    xmlns:local="clr-namespace:WpfApplication9" 
    Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
    <local:Person x:Key="myDataSource" /> 
    </Window.Resources> 
    <Grid> 
    <StackPanel> 
    <StackPanel.DataContext> 
     <Binding Source="{StaticResource myDataSource}"/> 
    </StackPanel.DataContext> 
    <WPFCtrl:FilterTextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged }"/> 
    <TextBlock Width="55" Height="25" Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"/> 
</StackPanel> 
</Grid> 

здесь Person Класс

namespace WpfApplication9 
{ 
    public class Person : INotifyPropertyChanged 
    { 
     private string name = ""; 
     // Declare the event 
     public event PropertyChangedEventHandler PropertyChanged; 

     public Person() 
     { 
     } 

     public Person(string value) 
     { 
      this.name = value; 
     } 

     public string Name 
     { 
      get { return name; } 
      set 
      { 
       name = value; 
       // Call OnPropertyChanged whenever the property is updated 
       OnPropertyChanged("Name"); 
      } 
     } 

     // Create the OnPropertyChanged method to raise the event 
     protected void OnPropertyChanged(string name) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(name)); 

      } 
     } 
    } 
} 

и текст FilterTextBox Имущество

public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(FilterTextBox), new PropertyMetadata()); 

    public string Text 
    { 
     //get { return _tbFilterTextBox.Text == null ? null : _tbFilterTextBox.Text.TrimEnd(); } 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
     //set { _tbFilterTextBox.Text = value; } 
    } 

Проблема в том, что она не входит в OnP ropertyChanged() Что я не так делаю?

ответ

1

ли обновление этого «FilterTextBox» Control текст DP каждый раз вставляется?

я думаю FilterTextBox имеет ControlTemplate с регулярной TextBox внутри. что-то вроде

<ControlTemplate TargetType="{x:Type FilterTextBox}"> 
<TextBox Name="PART_FilterTextBoxInputField" Text="{TemplateBinding Text}"/> 
</ControlTemplate> 

вам необходимо установить привязку где внутреннее текстовое поле связывается с вашим Text-Dependcy собственности использовать UpdateSourceTrigger = PropertyChanged тоже. в противном случае привязка будет обновляться только тогда, когда текстовое поле теряет фокус.

1

Проблема в том, что TextProperty в FilterTextBox не связывает TwoWay по умолчанию.

Либо установите BindingMode в TwoWay

<WPFCtrl:FilterTextBox Text="{Binding Path=Name, 
             Mode=TwoWay, 
             UpdateSourceTrigger=PropertyChanged }"/> 

Или изменить метаданные для DependencyPropertyText так он связывает TwoWay по умолчанию

public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", 
           typeof(string), 
           typeof(FilterTextBox), 
           new FrameworkPropertyMetadata(null, 
        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 
Смежные вопросы