问题
I'm writing an application with a ListView in C# WPF. I was wondering if I was missing a reference of something, because I get this error all the time:
'System.Windows.Controls.ListView' does not contain a definition for 'Columns' and no extension method 'Columns' accepting a first argument of type 'System.Windows.Controls.ListView' could be found (are you missing a using directive or an assembly reference?)`
It's not only with MyListview.Colums but also when I want to add a multi-column item to my ListView, for example by following this.
MyListView.Items.Add("Column1Text").SubItems.AddRange(row1); This gives me the same error for SubItems
It's hard for me to explain, so if something isn't clear you can ask.
Edit: With a lot of help from you and some Googling skills I found a solution. My XAML code:
<Grid>
<ListView x:Name="MyListView"
<ListView.View>
<GridView x:Name="MyGridView">
<GridViewColumn Header="#" DisplayMemberBinding="{Binding Number}" Width="24" />
<GridViewColumn Header="Song" DisplayMemberBinding="{Binding Song}" Width="390" />
</GridView>
</ListView.View>
</ListView>
and here is how to add an item:
MyListView.Items.Add(new { Number = 1, Song = "My first song" });
MyListView.Items.Add(new { Number = 2, Song = "My second song" });
This was my first time working with bindings, but I learned a lot!
回答1:
afaik, GridView has columns, ListView does not.
See MSDN:
Represents a view mode that displays data items in columns for a ListView control.
来源:https://stackoverflow.com/questions/10953503/missing-listview-columns-in-wpf