【问题标题】:Cannot bind WPF DependencyProperty无法绑定 WPF DependencyProperty
【发布时间】:2012-06-01 11:22:25
【问题描述】:

我是 WPF 新手,我正在尝试绑定依赖属性。 我希望我在 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>

这里是 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; }
    }

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

【问题讨论】:

    标签: wpf binding dependency-properties


    【解决方案1】:

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

    我猜 FilterTextBox 有一个 ControlTemplate,里面有一个常规的 TextBox。 像

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

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

    【讨论】:

      【解决方案2】:

      问题是FilterTextBox中的TextProperty默认不绑定TwoWay

      BindingMode 设置为TwoWay

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

      或者更改DependencyProperty Text 的元数据,使其默认绑定双向

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-12
        • 2011-05-31
        • 1970-01-01
        • 1970-01-01
        • 2013-01-20
        • 2011-08-06
        • 2017-01-28
        • 2011-01-17
        相关资源
        最近更新 更多