8

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.

Can you say what's wrong?

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");
            }
        }
    }
}
orberkov
  • 1,145
  • 2
  • 13
  • 27
  • 3
    DataContext="MainWindow" ??what do you think should happen when you do this? your datacontext is just a simple string called MainWindow. your output window should show you an exception also: Cannot find source for binding with reference 'ElementName=TheBook'. BindingExpression:Path=BookName; DataItem=null; target element is 'TextBox' (Name=''); target property is 'Text' (type 'String'). so pls give us some information what you wanna achieve. – blindmeis Jul 31 '13 at 10:56
  • also check http://msdn.microsoft.com/de-de/library/system.windows.data.binding.elementname.aspx because ElementName binding is not what you want/need this way – blindmeis Jul 31 '13 at 11:01

1 Answers1

7

First of all remove DataContext="MainWindow" as this sets DataContext of a Window to a string MainWindow, then you specify ElementName for 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=TheBook from your binding and either by assigning DataContext, which is default source if none is specified, of a Window to TheBook

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

or by specifying RelativeSource of your binding to the Window which exposes 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 TheBook into property:

public partial class SimpleBinding : Window
{
    public Book TheBook { get; set; }
    ...
}
dkozl
  • 32,814
  • 8
  • 87
  • 89
  • The main issue was: "You cannot bind to fields you will need to convert TheBook into property". In the XAML i fixed the TextBox binding like this: ``. The `DataContext="MainWindow"` remains. Thank you! – orberkov Jul 31 '13 at 11:04
  • 7
    @orberkov but `DataContext="MainWindow"` dose not set `DataContext` to an instance of `MainWindow` but to a string which value is _MainWindow_ – dkozl Jul 31 '13 at 11:08
  • @dkozl but `DataContext="MainWindow"` sets `DataContext` to something named `MainWindow` in the resulting CLR object. And since the `` has `x:Name="MainWindow"`, the `DataContext` is set to equal the CLR object itself. This is why `Path` ends up referencing the right property. (sorry for the 2 years delay, but noone seem to have addressed this comment) – Robert Jørgensgaard Engdahl Jan 13 '15 at 23:11
  • 2
    @RobertJørgensgaardEngdahl no it doesn't mean that. `DataContext="MainWindow"` will set it to _MainWindow_ string and nothing more. `DataContext="{Binding ElementName=MainWindow}"` will bind it to `x:Name="MainWindow"` control and `DataContext="{StaticResource MainWindow}"` will set it to `x:Key="MainWindow"` resource. You can check it in debugger. Besides I can have both resource and control called MainWindow. – dkozl Jan 14 '15 at 09:49