Noldorin
Noldorin

Reputation: 147240

Binding to the Current Item (WPF)

I am attempting to bind a ListView control to a DataTable, but the WPF binding system seems to be complaining about the binding path I specify.

As an example, a GridViewColumn is defined as follows:

<GridViewColumn Header="ColumnTitle" 
 DisplayMemberBinding="{Binding Path=/, 
                        Converter={StaticResource myConverter}}"/>

As far as I understand (and MSN seems to support me), specifying Path=/ should make the binding on the current item of the data collection.

The error I receive (in the trace window) is:

System.Windows.Data Error: 39 : BindingExpression path error: '' property not found on 'current item of collection' ''OrdersRow' (HashCode=680171)'. BindingExpression:Path=/; DataItem='OrdersRow' (HashCode=680171); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

This is giving me the impression that / isn't even a valid path, and WPF is expecting something after the slash. If so, how else would I bind to the current item? Why am I getting this error in the first place?

Upvotes: 5

Views: 11247

Answers (2)

Martin Harris
Martin Harris

Reputation: 28617

I think the confusion is that the DataContext for the GridViewColumn is not the top collection, but is already the item that is bound to that column, so you don't need to specify a path.

The time that the you may use a path like this is if your control's DataContext is a List and you want to bind to the selected item. A possible example would be.

<Combobox DataContext={Binding ColourList}
          DataSource={Binding} <!--Bind to the datacontext -->
          ForeColor={Binding/} <!--Bind to the currently selected item 
                                   in the datacontext -->
          />

Upvotes: 4

user7116
user7116

Reputation: 64068

Have you tried omitting the Path parameter?

<GridViewColumn Header="ColumnTitle"
     DisplayMemberBinding="{Binding Converter={StaticResource myConverter}}"/>

Upvotes: 8

Related Questions