简体   繁体   中英

Help with WPF data binding

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?

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 .

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:)

This might help:

http://blogs.msdn.com/b/wpfsdk/archive/2006/10/19/wpf-basic-data-binding-faq.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM