views:

48

answers:

2

If i bind a data table to WPF Toolkit-data-grid.....no need to specify the column names there...it will automatically take that from the datable.

But if I am binding a list-view to an observable collection...I have to specify the column header names for each column one by one..in xaml file.

So if I have a list of column names ->List<ColumnHeaderNames> along with list of item to populate ->List<Object to populate list>

I can bind item list to list-view...and column name list to list-view header...but i don't know if there s any property in list-view...to bind my header.

That means...

I have a user control in which i have 2 list-views ...one is available and another is selected. I need this user control shud be reusable...That means...if I am binding a list like ...the list shud contain two columns...first column with name as "state" and second column name as "county". But if I am binding a list like ..Then listview shud contain 3 columns...with column names as fruit, color and price.

+1  A: 

I think the best for you would be to set the View property of your ListView to a GridView. Then you can easily bind the columns header :

  <ListView ItemsSource="{Binding ListOfValues}">
            <ListView.View>
                <GridView >                
                    <GridViewColumn DisplayMemberBinding="{Binding XVal}" Header={binding header}"/>
                </GridView>
            </ListView.View>
        </ListView>

for more information you can go there http://msdn.microsoft.com/en-us/library/system.windows.controls.gridview.aspx

Gerrrard
You mean "header" in Header={binding header} is the list of columns.
Anish
if "header" is one of the items property in your list of item (as well as XVal for example), yes. Otherwise you can change the source of your binding for Header, but don't forget the DataContext is set on the current item of your list.
Gerrrard
I have more than one columns in source list...not single column. And I need to generate gridcolumns dynamically
Anish
oh sorry no I did not understand your question. I think, it would be much easier to add a property in your item (called header for example). Otherwise, as I said you have to change the source of the binding for Header
Gerrrard
How do you change source of grid??? My requirement is ...I have a user control in which i have 2 list-views ...one is available and another is selected. I need this user control shud be reusable...That means...if I am binding a list like <state,county>...the list shud contain two columns...first column with name as "state" and second column name as "county". But if I am binding a list like <fruit,color,price>..Then listview shud contain 3 columns...with column names as fruit, color and price.
Anish
A: 

And if you use a struct which contains the list and the name : < {listOfFruits,"Fruits"}, {listofStates,"States>

And then :

   <ItemsControl ItemsSource="{Binding ListOfStruct}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ListView ItemsSource="{Binding ListOfitem}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding}" Header="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Path=DataContext.Header}"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

It is not the best way but I don't know how to do

Gerrrard
What u do by this way?...Are you creating list of gridviews ?
Anish
list of listview where the view property is set to a gridViw wich contains only one column
Gerrrard