无法绑定WPF DependencyProperty

时间:2012-06-01 11:22:26

标签: wpf binding dependency-properties

我是WPF的新手,我尝试绑定一个dependacy属性。 我希望我在WPFCtrl上写的文本:FilterTextBox将在TextBlock中显示

这是我的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>

这里的人类

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

问题是它没有进入OnPropertyChanged() 我究竟做错了什么?

2 个答案:

答案 0 :(得分:1)

这个“FilterTextBox”控件是否每次插入文本时都会更新DP?

我想FilterTextBox里面有一个带有常规TextBox的ControlTemplate。

之类的东西
<ControlTemplate TargetType="{x:Type FilterTextBox}">
 <TextBox Name="PART_FilterTextBoxInputField" Text="{TemplateBinding Text}"/>
</ControlTemplate>

您需要设置内部文本框绑定到Text-Dependcy属性的绑定,以使用UpdateSourceTrigger = PropertyChanged。否则绑定只会在文本框失去焦点时更新。

答案 1 :(得分:1)

问题是TextProperty中的FilterTextBox默认情况下不会绑定TwoWay

BindingMode设置为TwoWay

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

或者更改DependencyProperty Text的元数据,以便默认情况下绑定twoway

public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text",
                                typeof(string),
                                typeof(FilterTextBox),
                                new FrameworkPropertyMetadata(null,
                     FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));