4

I have a collection of objects which I would like to represent columns in a GridView. Currently I am binding individual columns in my GridView to items in the collection like this:

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="{Binding Path=MyColumns[0].Title}"></GridViewColumn>
            <GridViewColumn Header="{Binding Path=MyColumns[1].Title}"></GridViewColumn>
            <GridViewColumn Header="{Binding Path=MyColumns[2].Title}"></GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

This works well, but what I really want to do is to bind to the collection itself (since the number of items in the collection might change at run-time). I would like to accomplish this in XAML. What I have in mind is something like this (note this does not work - the ColumnsSource attribute is make-believe):

<ListView>
    <ListView.View>
        <GridView ColumnsSource="{Binding Path=MyColumns}">
        </GridView>
    </ListView.View>
</ListView>

Can this be done? If so, how?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Greg Sansom
  • 20,442
  • 6
  • 58
  • 76
  • 1
    I found a duplicate at http://stackoverflow.com/q/1863200/503969 but the wording and presentation is quite different. – Greg Sansom Dec 09 '10 at 05:24

1 Answers1

2

You can use an attached property to create a ColumnsSource property, see this question for an example.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • @GregSansom: No, i am not talking about `ItemsSource`. You do not seem to have understood my point, see the question i linked to... – H.B. Aug 21 '12 at 23:58