【问题标题】:WPF Binding oneway : update property on source changed [duplicate]WPF单向绑定:更新源上的属性已更改[重复]
【发布时间】:2021-10-10 16:24:43
【问题描述】:

我有一个初学者问题。也许我很困惑,并没有完全弄清楚绑定概念。

我尝试使用绑定,但是当我更改源值时,我在文本框上看不到任何更新。

XLAM 文件:

<TextBox IsReadOnlyCaretVisible="True" AutomationProperties.Name="keyIdTextBox" Text="{Binding Path=Name, Mode=oneWay}"/>

...

<Button Content="Open" AutomationProperties.Name="openCloseButton" Click="OpenClose_Click"/>

C# 文件:

public class Person
{
    private string nameValue;
    public string Name
    {
        get { return nameValue; }
        set { nameValue = value; }
    }
}


/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private Person person = new Person { Name = "Bob" };

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = person;
    }


    private void OpenClose_Click(object sender, RoutedEventArgs e)
    {
        person.Name = "Lenny";
    }

在开始时,文本框更新良好并显示“Bob”,但是当我单击按钮更新名称的值时,文本框不反映值“Lenny”。没有变化。

感谢您的帮助。

【问题讨论】:

    标签: wpf data-binding


    【解决方案1】:

    选项 1:INotifyPropertyChanged

    Person 类应实现INotifyPropertyChanged 接口,以便在属性更改时通知目标。

    这是修改后的工作示例。

    public class Person : INotifyPropertyChanged
    {
        private string nameValue;
    
        public string Name
        {
            get { return nameValue; }
            set
            {
                nameValue = value;
                OnPropertyChanged();
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    public partial class MainWindow : Window
    {
        private Person person = new Person { Name = "Bob" };
    
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = person;
        }
    
        private void OpenClose_Click(object sender, RoutedEventArgs e)
        {
            person.Name = "Lenny";
        }
    }
    

    选项 2:DependencyProperty

    Name 属性定义为DependencyProperty。基础架构将处理这些更改。

    看看下面的例子。

    public class Person : DependencyObject
    {
        public static readonly DependencyProperty
            NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Person));
    
        public string Name
        {
            get => (string)GetValue(NameProperty);
            set => SetValue(NameProperty, value);
        }
    }
    
    public partial class MainWindow : Window
    {
        private Person person = new Person { Name = "Bob" };
    
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = person;
        }
    
        private void OpenClose_Click(object sender, RoutedEventArgs e)
        {
            person.Name = "Lenny";
        }
    }
    

    【讨论】:

    • 非常感谢.. 我认为 INotifyPropertyChanged 是 TwoWay 模式的一部分。我需要了解更多。
    • @Yaegaki2 不客气。我添加了另一个解决方法选项。如果它解决了您的问题,请接受我的回答。
    猜你喜欢
    • 1970-01-01
    • 2013-11-02
    • 2011-06-29
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    相关资源
    最近更新 更多