无法使用命令Add或SubItems.Add。

huangapple go评论76阅读模式
英文:

Unable to use the command Add or SubItems.Add

问题

I am trying to Add variables to the ListViewItem. It normally works with Windows Forms. Now I am trying to make it work with WPF. For some reason, the function Add or SubItems.Add to ListViewItem doesn't work. If you wonder, I do get data from the database. Any idea? Thanks already.

Using .NET 4.8

Error when hovering over Add: CS1061: 'ListViewItem' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'ListviewItem' could be found (are you missing a using directive or an assembly reference?)

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using WPF_Application.Controller;
using WPF_Application.Model;
using WPF_Application.View;

namespace WPF_Application
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        VraagController vraagController = new VraagController();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {

            FillListView();
        }

        public void FillListView()
        {
            List<VraagModel> vraagList = vraagController.ReadAll();

            vragenLijst.Items.Clear();

            foreach (VraagModel item in vraagList)
            {
                // Create listview item

                ListViewItem vraaglijst = new ListViewItem();
                vraaglijst.Parameters.AddWithValue(item.Vraagstelling);
                vraaglijst.Add(item.Correctantwoord);
                vraaglijst.Add(item.Meerkeuzevraag.Antwoord1);
                vraaglijst.Add(item.Meerkeuzevraag.Antwoord2);
                vraaglijst.Add(item.Meerkeuzevraag.Antwoord3);
                vraaglijst.Add(item.Meerkeuzevraag.Antwoord4);

                vraaglijst.Tag = item;

                vragenLijst.Items.Add(vraaglijst);

            }
        }
    }
}

MainWindow.xaml

<Window x:Class="WPF_Application.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_Application"
        Name="mainWindow"
        mc:Ignorable="d"
        Title="Bedrijfsnaam" Height="450" Width="800">
    <Grid Background="#F5F5F5" Loaded="Grid_Loaded">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="131*"/>
            <ColumnDefinition Width="669*"/>
        </Grid.ColumnDefinitions>
        <Grid Background="White" Margin="25,40,25,15" Grid.ColumnSpan="2" >
            <TextBox Name="FilterTextBox"  Height="30" Width="200" Margin="25" VerticalAlignment="Top"  HorizontalAlignment="left" BorderThickness="1" BorderBrush="#E0E0E0" />
            <Button Height="30" Click="btnToevoegen" Content="Toevoegen" Width="100" VerticalContentAlignment="Center" Padding="5" Margin="0,25,130,0" VerticalAlignment="Top" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="#E0E0E0"  />
            <Button Height="30" Click="btnWijzigen" Content="Wijzigen" Width="100" VerticalContentAlignment="Center" Padding="5" Margin="0,25,235,0" VerticalAlignment="Top" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="#E0E0E0"  />
            <Button Height="30" Content="Verwijderen" Width="100" VerticalContentAlignment="Center" Padding="5" Margin="0,25,25,0" VerticalAlignment="Top" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="#E0E0E0"  />
            <ListView Name="vragenLijst" BorderBrush="#cdcdcd" Margin="25,70,25,25" Padding="0">
                <ListView.View>
                    <GridView>
                        <GridViewColumn x:Name="Vraagstelling" Header="Vraagstelling" Width="200"/>
                        <GridViewColumn x:Name="Correctantwoord" Header="Correct antwoord" Width="100"/>
                        <GridViewColumn x:Name="Antwoord1" Header="Antwoord 1" Width="100"/>
                        <GridViewColumn x:Name="Antwoord2" Header="Antwoord 2" Width="100"/>
                        <GridViewColumn x:Name="Antwoord3" Header="Antwoord 3" Width="100"/>
                        <GridViewColumn x:Name="Antwoord4" Header="Antwoord 4" Width="100"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Grid>
</Window>
英文:

I'am trying to Add variables to the ListViewItem. It normally work with Windows Forms. Now i am trying to make it work with wpf. For some reason the function Add or SubItems.Add to ListviewItem doesn't work. If you wonder i do get data from the database. Any idea. Thanks already.

Using net 4.8

Error when hovering over Add: CS1061: 'ListViewItem' does not contain definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'ListviewItem' could be found (are you missing a using directive or an assembly reference?)

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using WPF_Application.Controller;
using WPF_Application.Model;
using WPF_Application.View;
namespace WPF_Application
{
/// &lt;summary&gt;
/// Interaction logic for MainWindow.xaml
/// &lt;/summary&gt;
public partial class MainWindow : Window
{
VraagController vraagController = new VraagController();
public MainWindow()
{
InitializeComponent();
}
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
FillListView();
}
public void FillListView()
{
List&lt;VraagModel&gt; vraagList = vraagController.ReadAll();
vragenLijst.Items.Clear();
foreach (VraagModel item in vraagList)
{
//listview item aanmaken
ListViewItem vraaglijst = new ListViewItem();
vraaglijst.Parameters.AddWithValue(item.Vraagstelling);
vraaglijst.Add(item.Correctantwoord);
vraaglijst.Add(item.Meerkeuzevraag.Antwoord1);
vraaglijst.Add(item.Meerkeuzevraag.Antwoord2);
vraaglijst.Add(item.Meerkeuzevraag.Antwoord3);
vraaglijst.Add(item.Meerkeuzevraag.Antwoord4);
vraaglijst.Tag = item;
vragenLijst.Items.Add(vraaglijst);
}
}
}
}

MainWindow.xaml

&lt;Window x:Class=&quot;WPF_Application.MainWindow&quot;
xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
xmlns:local=&quot;clr-namespace:WPF_Application&quot;
Name=&quot;mainWindow&quot;
mc:Ignorable=&quot;d&quot;
Title=&quot;Bedrijfsnaam&quot; Height=&quot;450&quot; Width=&quot;800&quot;&gt;
&lt;Grid Background=&quot;#F5F5F5&quot; Loaded=&quot;Grid_Loaded&quot;&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition Width=&quot;131*&quot;/&gt;
&lt;ColumnDefinition Width=&quot;669*&quot;/&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;Grid Background=&quot;White&quot; Margin=&quot;25,40,25,15&quot; Grid.ColumnSpan=&quot;2&quot; &gt;
&lt;TextBox Name=&quot;FilterTextBox&quot;  Height=&quot;30&quot; Width=&quot;200&quot; Margin=&quot;25&quot; VerticalAlignment=&quot;Top&quot;  HorizontalAlignment=&quot;left&quot; BorderThickness=&quot;1&quot; BorderBrush=&quot;#E0E0E0&quot; /&gt;
&lt;Button Height=&quot;30&quot; Click=&quot;btnToevoegen&quot; Content=&quot;Toevoegen&quot; Width=&quot;100&quot; VerticalContentAlignment=&quot;Center&quot; Padding=&quot;5&quot; Margin=&quot;0,25,130,0&quot; VerticalAlignment=&quot;Top&quot; HorizontalAlignment=&quot;Right&quot; BorderThickness=&quot;1&quot; BorderBrush=&quot;#E0E0E0&quot;  /&gt;
&lt;Button Height=&quot;30&quot; Click=&quot;btnWijzigen&quot; Content=&quot;Wijzigen&quot; Width=&quot;100&quot; VerticalContentAlignment=&quot;Center&quot; Padding=&quot;5&quot; Margin=&quot;0,25,235,0&quot; VerticalAlignment=&quot;Top&quot; HorizontalAlignment=&quot;Right&quot; BorderThickness=&quot;1&quot; BorderBrush=&quot;#E0E0E0&quot;  /&gt;
&lt;Button Height=&quot;30&quot; Content=&quot;Verwijderen&quot; Width=&quot;100&quot; VerticalContentAlignment=&quot;Center&quot; Padding=&quot;5&quot; Margin=&quot;0,25,25,0&quot; VerticalAlignment=&quot;Top&quot; HorizontalAlignment=&quot;Right&quot; BorderThickness=&quot;1&quot; BorderBrush=&quot;#E0E0E0&quot;  /&gt;
&lt;ListView Name=&quot;vragenLijst&quot; BorderBrush=&quot;#cdcdcd&quot; Margin=&quot;25,70,25,25&quot; Padding=&quot;0&quot;&gt;
&lt;ListView.View&gt;
&lt;GridView&gt;
&lt;GridViewColumn x:Name=&quot;Vraagstelling&quot; Header=&quot;Vraagstelling&quot; Width=&quot;200&quot;/&gt;
&lt;GridViewColumn x:Name=&quot;Correctantwoord&quot; Header=&quot;Correct antwoord&quot; Width=&quot;100&quot;/&gt;
&lt;GridViewColumn x:Name=&quot;Antwoord1&quot; Header=&quot;Antwoord 1&quot; Width=&quot;100&quot;/&gt;
&lt;GridViewColumn x:Name=&quot;Antwoord2&quot; Header=&quot;Antwoord 2&quot; Width=&quot;100&quot;/&gt;
&lt;GridViewColumn x:Name=&quot;Antwoord3&quot; Header=&quot;Antwoord 3&quot; Width=&quot;100&quot;/&gt;
&lt;GridViewColumn x:Name=&quot;Antwoord4&quot; Header=&quot;Antwoord 4&quot; Width=&quot;100&quot;/&gt;
&lt;/GridView&gt;
&lt;/ListView.View&gt;
&lt;/ListView&gt;
&lt;/Grid&gt;
&lt;/Grid&gt;
&lt;/Window&gt;

答案1

得分: 2

没有ListViewItems.SubItems属性(这不是WinForms!),也没有ListViewItem.Add属性。你得到的错误消息明确指出了:

> CS1061: 'ListViewItem'不包含定义'Add'的内容

这可能是最好的错误消息了。

当你不确定类的API时,必须查阅.NET API参考文档,并查找例如ListViewListViewItem。在属性部分,你可以找到所有公共属性。

而是必须使用ListView.ItemsSource属性。
这个属性保存包含ListView(或通常的ItemsControl)必须显示的项目的源集合。

要定义GridView的列,你必须将GridViewColumn.DisplayMemberBinding属性设置为指向数据项上相关属性的属性路径。

.NET文档还提供了有助于了解框架和特定控件的重要主题。例如,阅读Microsoft Docs: ListView Overview将为你提供如何使用ListView的概念(例如如何分配数据源或如何定义GridView的列)。

public void FillListView()
{
  List&lt;VraagModel&gt; vraagList = vraagController.ReadAll();

  // 不是必需的。避免性能成本。
  //vragenLijst.Items.Clear();

  // 只需将IEnumerable分配给ListView.ItemsSource
  vragenLijst.ItemsSource = vraagList;
}
&lt;ListView Name=&quot;vragenLijst&quot;&gt;
&lt;ListView.View&gt;
&lt;GridView&gt;
&lt;GridViewColumn Header=&quot;Vraagstelling&quot; 
DisplayMemberBinding=&quot;{Binding Vraagstelling}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Correct antwoord&quot;
DisplayMemberBinding=&quot;{Binding Correctantwoord}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Antwoord 1&quot;
DisplayMemberBinding=&quot;{Binding Meerkeuzevraag.Antwoord1}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Antwoord 2&quot;
DisplayMemberBinding=&quot;{Binding Meerkeuzevraag.Antwoord2}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Antwoord 3&quot;
DisplayMemberBinding=&quot;{Binding Meerkeuzevraag.Antwoord3}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Antwoord 4&quot;
DisplayMemberBinding=&quot;{Binding Meerkeuzevraag.Antwoord4}&quot; /&gt;
&lt;/GridView&gt;
&lt;/ListView.View&gt;
&lt;/ListView&gt;
英文:

There is no ListViewItems.SubItems property (this is not WinForms!) and there is no ListViewItem.Add property. The error message you get clearly states that:

> CS1061: 'ListViewItem' does not contain definition for 'Add'

There couldn't be a better error message.

When you are not sure about the class API you must consult the .NET API reference and lookup the e.g. ListView or ListViewItem. In the properties section you can find all public properties.

Instead you must use the ListView.ItemsSource property.
This property holds the source collection that contains the items that the ListView (or ItemsControl in general) has to display.

To define the columns of the GridView you must set the GridViewColumn.DisplayMemberBinding property to a property path that points to the associated property on the data item.

The .NET documentation also provides important topics that help to learn about the framework and particular controls. For example reading Microsoft Docs: ListView Overview would give you an idea how to use the ListView (for example how assign the data source or how to define the columns of the GridView).

public void FillListView()
{
  List&lt;VraagModel&gt; vraagList = vraagController.ReadAll();

  // Not required. Avoid the performance cost.
  //vragenLijst.Items.Clear();

  // Simply assign the IEnumerable to the ListView.ItemsSource
  vragenLijst.ItemsSource = vraagList;
}
&lt;ListView Name=&quot;vragenLijst&quot;&gt;
&lt;ListView.View&gt;
&lt;GridView&gt;
&lt;GridViewColumn Header=&quot;Vraagstelling&quot; 
DisplayMemberBinding=&quot;{Binding Vraagstelling}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Correct antwoord&quot;
DisplayMemberBinding=&quot;{Binding Correctantwoord}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Antwoord 1&quot;
DisplayMemberBinding=&quot;{Binding Meerkeuzevraag.Antwoord1}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Antwoord 2&quot;
DisplayMemberBinding=&quot;{Binding Meerkeuzevraag.Antwoord2}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Antwoord 3&quot;
DisplayMemberBinding=&quot;{Binding Meerkeuzevraag.Antwoord3}&quot; /&gt;
&lt;GridViewColumn Header=&quot;Antwoord 4&quot;
DisplayMemberBinding=&quot;{Binding Meerkeuzevraag.Antwoord4}&quot; /&gt;
&lt;/GridView&gt;
&lt;/ListView.View&gt;
&lt;/ListView&gt;

huangapple
  • 本文由 发表于 2023年6月29日 02:47:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定