为什么我的C#代码不会添加到列表中并显示它?

时间:2012-11-14 02:26:59

标签: c# wpf list addressbook

我正在尝试自学C#,但我遇到了这段代码的问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            contacts.Add(new Contact()
                {
                Name = "James",
                Email = "james@mail.com",
                PhoneNumber = "01234 111111"
            });
            contacts.Add(new Contact()
            {
                Name = "Bob",
                Email = "bob@mail.com",
                PhoneNumber = "01234 222222"
            });
            contacts.Add(new Contact()
            {
                Name = "Emma",
                Email = "emma@mail.com",
                PhoneNumber = "01234 333333"
            });
        }
        protected List<Contact>  contacts = new List<Contact>();

        public List<Contact> Contacts
        {
            get { return  contacts; }
            set {  contacts = value; }
        }

        private void NewContactButton_Click(object sender, RoutedEventArgs e)
        {
            contacts.Add(new Contact()
            {
                Name = NameTextBox.Text,
                Email = EmailTextBox.Text,
                PhoneNumber = PhoneTextBox.Text
            });
        }

        }

    }

它没有在列表中显示新联系人,我不确定它是否正在创建新联系人。它显示前三个非常好。

我有一种感觉,我遗漏了一些重要的东西。

2 个答案:

答案 0 :(得分:5)

更改

   protected List<Contact>  contacts = new List<Contact>();

    public List<Contact> Contacts
    {
        get { return  contacts; }
        set {  contacts = value; }
    }

protected ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();

public ObservableCollection<Contact> Contacts
{
    get { return contacts; }
    set { contacts = value; }
}

并将using System.Collections.ObjectModel;添加到代码顶部。

并对其余代码进行必要的更改。

答案 1 :(得分:1)

使用下面提到的代码,它将起作用:

public MainWindow()
    {
        InitializeComponent();
        dgContacts.ItemsSource = Contacts;
    }

    private void btnClick_Click(object sender, RoutedEventArgs e)
    {
        Contacts.Add(new contact()
        {
            Name = "Person",
            Email = "Person Address",
            PhoneNumber = "Person Ph"
        });

    }

    protected ObservableCollection<contact> contacts = new ObservableCollection<contact>();

    public ObservableCollection<contact> Contacts
    {
        get { return contacts; }
        set { contacts = value; }
    }

此代码的xaml将是:

<DataGrid Name="dgContacts" Margin="5" AutoGenerateColumns="True" ItemsSource="{Binding}"></DataGrid>

WPF的主要部分是绑定,如果你正确地进行绑定,它将为你创造魔力。如果此代码适合您;我将告诉你另一种绑定它的方法。