4

I'm new to WPF and I'm trying to figure out how data binding works, but I'm not having much luck.

I'm trying to start with something simple - binding the contents of a text box to a string variable in my program.

I read lots and lots of pages of MSDN documentation about data binding, XML namespaces, markup extensions, resources, dependency properties and whatnot, and I'm still not able to get it to work.

Here's my MainWindow.xaml:

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:WpfTest"
        Title="MainWindow">
    <Grid>
        <Grid.Resources>
            <c:Foo x:Key="MyFoo"/>
        </Grid.Resources>
        <TextBox Width="100" Height="28"
                 Text="{Binding Source=MyFoo,
                                Path=BarProperty,
                                Mode=TwoWay,
                                UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

And my MainWindow.xaml.cs:

namespace WpfTest
{
    public class Foo : DependencyObject
    {
        public static readonly DependencyProperty BarProperty = DependencyProperty.Register("Bar", typeof(String), typeof(Foo));

        public String Bar
        {
            get { return (String)GetValue(BarProperty); }
            set { SetValue(BarProperty, value); }
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyFoo = new Foo { Bar = "hello" };
        }

        public Foo MyFoo { get; set; }
    }
}

I would expect the text box to show "hello" when the program starts up, but it is empty.

Can someone tell me what I am doing wrong?

3
  • If you had read the Data Binding Overview you probably would have been able to figure everything out yourself..... Commented Jun 13, 2011 at 20:43
  • @H.B. I did read the Data Binding Overview, as well as numerous other MSDN documentation pages. In fact I was basing my code on the example under "Creating a Binding", but it did not work! All of their example used this Grid.Resources or DockPanel.Resources etc. section, which just screws things up for me. Commented Jun 13, 2011 at 20:48
  • Well, it is admittedly a common mistake to just type the key of a resource without the StaticResourceExtension or a class name which in both cases makes the Source a string. You can create your source right in XAML by the way, but you cannot use the binding markup extension for that but an actual Binding object where you then can specify the source in XML-element syntax. Commented Jun 13, 2011 at 20:51

2 Answers 2

3

You need to set the DataContext of your Window to itself.

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
    MyFoo = new Foo { Bar = "hello" };
}

This tells WPF to look for bindings within your class.

Every control can set a DataContext which says "when I bind, I want to bind to a property on this specific instance... This is inherited, so if you set the DataContext of the MainWindow to itself, all controls inside of MainWindow will bind to properties on the MainWindow.

Sign up to request clarification or add additional context in comments.

7 Comments

Doesn't help... text box is still empty
UPDATE: It works if I do that and remove the binding's Source property, and change its Path to MyFoo.Bar.
@HighCommander4: Was just going to note that you need to remove the source (other sources which can be used are RelativeSource and ElementName, if any is specified it will be used instead of the DataContext).
@HighCommander4: Yes, the source was overriding the DataContext. Sorry about missing that in there..
Can you explain what the Grid.Resources section does? I'm finding I don't need it, but it's there in all the MSDN examples.
|
1

You need to specify the source. Either:

Give the window a name like Name="mywin", alter your binding witn ElementName="myWin"

Or set the window DataContext like:

DataContext="{Binding ElementName="myWin"} - you can also use a RelativeSource if you don't want the name I just couldn't post it untested - Bindings tend to require testing as you also noticed :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.