如何在listview中添加项目

时间:2018-03-27 20:07:27

标签: c# wpf listview

我有ListView,在右侧textBoxes,Combobox和Add Button,我需要在listview中添加项目,一切似乎都可以,但是当我调试它并点击按钮添加时,它会给我这样的错误 http://prntscr.com/ix7zgg

这是我的WPF` http://prntscr.com/ix83xi

我写错了什么?这是我的代码` http://prntscr.com/ix81oe

<Window x:Class="Suren_WPF_Controls.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:Suren_WPF_Controls"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="700">
<Grid>
    <ListView x:Name="listview" Margin="0,0,300,50">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Anun" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Header="Azganun" DisplayMemberBinding="{Binding Surname}" />
                <GridViewColumn Header="Tariq" DisplayMemberBinding="{Binding Age}" />
                <GridViewColumn Header="Masnajyux" DisplayMemberBinding="{Binding Branch}" />
            </GridView>
        </ListView.View>
    </ListView>
    <TextBlock Margin="420 10 0 0" Text="Name"/>
    <TextBox x:Name="nametextBox" Margin="420,30,72,260"/>
    <TextBlock Margin="420 70 0 0" Text="Surname"/>
    <TextBox x:Name="surnametextBox" Margin="420,90,72,200"/>
    <TextBlock Margin="420 130 0 0" Text="Age"/>
    <TextBox x:Name="agetextBox" Margin="420,150,72,140"/>
    <TextBlock Margin="420 190 0 0" Text="Branch"/>
    <ComboBox x:Name="branchcomboBox" Margin="420,210,72,80">
        <ComboBoxItem>Painted</ComboBoxItem>
        <ComboBoxItem> Not Painted</ComboBoxItem>
    </ComboBox>
    <Button x:Name="btnAdd" Content="Add" Click="btnAdd_Click" Margin="570,275,20,10"/>
</Grid>

public partial class MainWindow : Window
{
    public List<User> users;
    public MainWindow()
    {
        InitializeComponent();
        users = new List<User>()
        {
            new User("Petros","Petrosyan",20,"Komitas"),
            new User("Poghos","Poghosyan",28,"Tigran Mets"),
            new User("Valod","Hakobyan",36,"Baghramyan"),
            new User("Mher","Simonyan",19,"Amiryan"),
            new User("Ruben","Kirakosyan",22,"Kentron"),
        };
        listview.ItemsSource = users;
    }
    private void add()
    {
        users = new List<User> { new User(nametextBox.Text, surnametextBox.Text, int.Parse(agetextBox.Text), branchcomboBox.SelectedItem.ToString()) };
        listview.Items.Add(users);
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
       this.add();
    }
}
public class User
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public int Age { get; set; }
    public string Branch { get; set; }
    public User(string Name,string Surname,int Age,string Branch)
    {
        this.Name = Name;
        this.Surname = Surname;
        this.Age = Age;
        this.Branch = Branch;
    }
}

2 个答案:

答案 0 :(得分:0)

您似乎正在尝试使用两种不同的机制将项目添加到ListView中。一个 - 通过ItemsSource,另一个直接通过Items。在构造函数中,您为ItemsSource分配了一个列表,但实际上您尝试修改这些项目。

您有两种方法可以实现您的目标:  1.仅使用Items,并将User实例直接添加到Items属性。  2.坚持使用DataBinding并使用ItemsSource。将其分配给ObservableColleciton<User>并在您的add方法中只添加一个项目到基础集合 - 它将自动使用添加的项目填充列表。以下答案包含有关如何准确连线的详细信息:WPF ListView Binding ItemsSource in XAML

答案 1 :(得分:0)

正如其他答案所建议的那样,您应该使用Observable Collection并将其绑定为列表视图的项目源。 您应该使用TryParse方法来解析年龄,以避免异常。 您应该为您使用try catch块添加方法,以便在异常情况下您的应用程序不会崩溃。我现在正在消息框中显示异常。

var users = new ObservableCollection<User>();

            private void Add()
            {
                try
                {
                    int age = 0;
                    if (int.TryParse(agetextBox.Text.ToString(), out int parsedAge))
                    {
                        age = parsedAge;
                    }

                    users.Add(new User
                    {
                        Name = nametextBox.Text,
                        Surname = surnametextBox.Text,
                        Age = age,
                        Branch = branchcomboBox.SelectedItem.ToString()
                    };
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }