Missing ListView.Columns in WPF

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:27:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!