Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 729
Noldorin Avatar asked Jul 16 '09 15:07

Noldorin


2 Answers

Have you tried omitting the Path parameter?

<GridViewColumn Header="ColumnTitle"
     DisplayMemberBinding="{Binding Converter={StaticResource myConverter}}"/>
like image 138
user7116 Avatar answered Sep 28 '22 06:09

user7116


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 -->
          />
like image 36
Martin Harris Avatar answered Sep 28 '22 05:09

Martin Harris