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>
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>