WPF Einfache Bindung an die INotifyPropertyChanged-Objekt

Habe ich die einfachste Bindung. Ein Textfeld gebunden, um ein Objekt in der code-behind.

Ereignis, obwohl - das Textfeld bleibt leer.

Des Fensters DataContext gesetzt ist, und die Bindung Pfad vorhanden ist.

Können Sie sagen, was ist falsch?

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 hinter

   public partial class SimpleBinding : Window
    {
        public Book TheBook;

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

Dem Buch-Objekt

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");
            }
        }
    }
}
DataContext="MainWindow" ??was denkst du, was passieren soll, wenn Sie dies tun? datacontext ist nur eine einfache Zeichenfolge heißt MainWindow. Ihre Ausgabefenster sollten Sie eine Ausnahme auch: Nicht finden können, die Quelle für die Bindung mit der Referenz 'ElementName=TheBook'. BindingExpression:Path=BookName; DataItem=null; target element is 'TextBox' (Name="); target-Eigenschaft 'Text' (Typ 'String'). also pls geben Sie uns einige Informationen, was Sie wollen, zu erreichen.
überprüfen Sie auch msdn.microsoft.com/de-de/library/..., weil ElementName-Bindung ist nicht das, was Sie wollen/müssen diesem Weise

InformationsquelleAutor orberkov | 2013-07-31

Schreibe einen Kommentar