tags:

views:

260

answers:

1

Hi guys, I might sound dumb here, but I want to do something really simple. At design time, I want to add columns to a listview control and add some data to it. I need to add combobox in each column of the listview. The thing I am not able to find is where to mention the column number in the listviewitem. Any help appreciated guys.

<ListView IsSynchronizedWithCurrentItem="True" Margin="8,68,304,188"  
      BorderThickness="2,2,2,2">
 <ListView.View>
   <GridView>
    <GridViewColumn Width="150" Header="Column1"/>
    <GridViewColumn Width="150" Header="Column2"/>
   </GridView>
 </ListView.View>
 <ListViewItem>                              
 </ListViewItem>            
</ListView>
+2  A: 

Each column in the listviewitem is rendered based on the GridView definition, so there is no real concept of column numbers. What you do is bind objects to the the listview's itemsource and it creates listviewitems from it. Thus, there are a few hoops to jump through.

This link has an example of how to do some simple object data binding. The advantage of this is what binding structure you have for design time can probably be reused for run-time if you set the datacontext/itemsource to an empty object instead of the static one in XAML.

If you're doing this to show examples or you just have a static data source that you want to use, I would recommend using the XmlDataProvider. Then you'd change your ListView to be like this,


<ListView IsSynchronizedWithCurrentItem="True" Margin="8,68,304,188"  
      BorderThickness="2,2,2,2">
 <ListView.View>
   <GridView>
    <GridViewColumn Width="150" Header="Column1" DisplayMemberPath="{Binding XPath=/A/B}"/>
    <GridViewColumn Width="150" Header="Column2" DisplayMemberPath="{Binding XPath=/A/C"/>
   </GridView>
 </ListView.View>
 <ListViewItem>                              
 </ListViewItem>            
</ListView>

apandit
Ya, I guess I was searching something which never existed :). Binding is the way to go. Thanks.
theraneman