Syntax In WPF Data Binding RRS feed

  • Question

  • Hello, I have a piece of code that contains a DataTemplate with a TextBlock and a couple of other controls. The DataTemplate is used for a GridViewColumnHeader in a GridView/ListView.

    The TextBlock has its Text property set like this...


    ...now, I get the whole concept of using data binding with a source, a target, a mode (e.g. "One-way") etc...

    But what is going on when one uses the above syntax?

    Suppose I wanted to use the full syntax, say for clarity? The source gridview column has its header set like...

    Header="My Header"

    ...how would I set that up?

    Thursday, April 1, 2010 6:45 PM


  • When you use Text="{Binding}", what it does is sets up a binding to the current DataContext.


    Normally, you'd do something like:


         Text="{Binding Path=MyProperty}"


    This binds to the "MyProperty" property on the current DataContext.  If you want to bind to the DataContext itself (not a property of it), then you can just use "{Binding}".


    A nice reference for seeing this, and other binding related issues, is the WPF Data Binding CheatSheet.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Linda Liu Monday, April 5, 2010 3:47 AM
    Thursday, April 1, 2010 6:59 PM