英文:
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
{
/// <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)
{
//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
<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>
答案1
得分: 2
没有ListViewItems.SubItems
属性(这不是WinForms!),也没有ListViewItem.Add
属性。你得到的错误消息明确指出了:
> CS1061: 'ListViewItem'不包含定义'Add'的内容
这可能是最好的错误消息了。
当你不确定类的API时,必须查阅.NET API参考文档,并查找例如ListView
或ListViewItem
。在属性部分,你可以找到所有公共属性。
而是必须使用ListView.ItemsSource
属性。
这个属性保存包含ListView
(或通常的ItemsControl
)必须显示的项目的源集合。
要定义GridView
的列,你必须将GridViewColumn.DisplayMemberBinding
属性设置为指向数据项上相关属性的属性路径。
.NET文档还提供了有助于了解框架和特定控件的重要主题。例如,阅读Microsoft Docs: ListView Overview将为你提供如何使用ListView
的概念(例如如何分配数据源或如何定义GridView
的列)。
public void FillListView()
{
List<VraagModel> vraagList = vraagController.ReadAll();
// 不是必需的。避免性能成本。
//vragenLijst.Items.Clear();
// 只需将IEnumerable分配给ListView.ItemsSource
vragenLijst.ItemsSource = vraagList;
}
<ListView Name="vragenLijst">
<ListView.View>
<GridView>
<GridViewColumn Header="Vraagstelling"
DisplayMemberBinding="{Binding Vraagstelling}" />
<GridViewColumn Header="Correct antwoord"
DisplayMemberBinding="{Binding Correctantwoord}" />
<GridViewColumn Header="Antwoord 1"
DisplayMemberBinding="{Binding Meerkeuzevraag.Antwoord1}" />
<GridViewColumn Header="Antwoord 2"
DisplayMemberBinding="{Binding Meerkeuzevraag.Antwoord2}" />
<GridViewColumn Header="Antwoord 3"
DisplayMemberBinding="{Binding Meerkeuzevraag.Antwoord3}" />
<GridViewColumn Header="Antwoord 4"
DisplayMemberBinding="{Binding Meerkeuzevraag.Antwoord4}" />
</GridView>
</ListView.View>
</ListView>
英文:
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<VraagModel> vraagList = vraagController.ReadAll();
// Not required. Avoid the performance cost.
//vragenLijst.Items.Clear();
// Simply assign the IEnumerable to the ListView.ItemsSource
vragenLijst.ItemsSource = vraagList;
}
<ListView Name="vragenLijst">
<ListView.View>
<GridView>
<GridViewColumn Header="Vraagstelling"
DisplayMemberBinding="{Binding Vraagstelling}" />
<GridViewColumn Header="Correct antwoord"
DisplayMemberBinding="{Binding Correctantwoord}" />
<GridViewColumn Header="Antwoord 1"
DisplayMemberBinding="{Binding Meerkeuzevraag.Antwoord1}" />
<GridViewColumn Header="Antwoord 2"
DisplayMemberBinding="{Binding Meerkeuzevraag.Antwoord2}" />
<GridViewColumn Header="Antwoord 3"
DisplayMemberBinding="{Binding Meerkeuzevraag.Antwoord3}" />
<GridViewColumn Header="Antwoord 4"
DisplayMemberBinding="{Binding Meerkeuzevraag.Antwoord4}" />
</GridView>
</ListView.View>
</ListView>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论