开发者

populate listview dynamically

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 use开发者_运维百科r 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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜