WPF 简单绑定到 INotifyPropertyChanged 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17967052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:52:58  来源:igfitidea点击:

WPF Simple Binding to INotifyPropertyChanged Object

wpfdata-bindingdatacontextinotifypropertychanged

提问by orberkov

I've created the simplest binding. A textbox bound to an object in the code behind.

我已经创建了最简单的绑定。绑定到后面代码中的对象的文本框。

Event though - the textbox remains empty.

事件 - 文本框保持为空。

The window's DataContext is set, and the binding path is present.

设置了窗口的 DataContext,并且存在绑定路径。

Can you say what's wrong?

你能说有什么问题吗?

XAML

XAML

<Window x:Class="Anecdotes.SimpleBinding"
        x:Name="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SimpleBinding" Height="300" Width="300" DataContext="MainWindow">
    <Grid>
        <TextBox Text="{Binding Path=BookName, ElementName=TheBook}" />
    </Grid>
</Window>

Code behind

背后的代码

   public partial class SimpleBinding : Window
    {
        public Book TheBook;

        public SimpleBinding()
        {
            TheBook = new Book() { BookName = "The Mythical Man Month" };
            InitializeComponent();
        }
    }

The book object

本书对象

public class Book : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

    }

    private string bookName;

    public string BookName
    {
        get { return bookName; }
        set
        {
            if (bookName != value)
            {
                bookName = value;
                OnPropertyChanged("BookName");
            }
        }
    }
}

采纳答案by dkozl

First of all remove DataContext="MainWindow"as this sets DataContextof a Windowto a stringMainWindow, then you specify ElementNamefor your binding which defines binding source as another control with x:Name="TheBook"which does not exist in your Window. You can make your code work by removing ElementName=TheBookfrom your binding and either by assigning DataContext, which is default source if none is specified, of a Windowto TheBook

首先DataContext="MainWindow"DataContextaWindow设置为stringMainWindow,然后ElementName为您的绑定指定绑定源,该绑定将绑定源定义为另一个控件,x:Name="TheBook"该控件在您的Window. 您可以通过删除使你的代码的工作ElementName=TheBook由指定的结合,要么DataContext,如果没有指定,这是默认信号源的WindowTheBook

public SimpleBinding()
{
    ...
    this.DataContext = TheBook;
} 

or by specifying RelativeSourceof your binding to the Windowwhich exposes TheBook:

或通过指定RelativeSource您的绑定到Windowwhich 暴露TheBook

<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=TheBook.BookName}"/>

but since you cannot bind to fields you will need to convert TheBookinto property:

但由于您无法绑定到字段,因此您需要转换TheBook为属性:

public partial class SimpleBinding : Window
{
    public Book TheBook { get; set; }
    ...
}