为了账号安全,请及时绑定邮箱和手机立即绑定

重新加载页面时出现数据网格错误

重新加载页面时出现数据网格错误

C#
慕雪6442864 2021-10-24 18:01:52
我有一个添加客户 UI,它应用了一个数据网格来显示提供的条目,但是如果我在已经显示的同时重新选择添加客户,它将再次添加条目。第 1-3 行是原始行,第 4-6 行是重复行。有谁知道如何限制这种情况的发生?在我的 MainActivity 中,我有一个带有 Frame 的菜单导航来填充 xaml 页面。下面是初始化类的代码。private void AddCustomer_Click(object sender, RoutedEventArgs e){    Main.Content = new AddCustomerUI();}Frame 被称为 Main,然后将 xaml 页面分配给它的 Main.contentpublic partial class AddCustomerUI : Page{    public AddCustomerUI()    {        InitializeComponent();        SetupCustomer();        customerDataGrid.ItemsSource = App.customers;    }    private void SetupCustomer()    {        var customer = new Customer { FirstName = "Timothy", LastName = "Jennings", Email = "tim.jennings@gmail.com", Phone = "0275 202020",            Address = new Address { Street = "100 Burt Road", Suburb = "Howick", City = "Auckland", Country = "New Zealand" }        };        App.customers.Add(customer);        customer = new Customer { FirstName = "Brian", LastName = "Jones", Email = "bjones@gmail.com", Phone = "0275 903070",            Address = new Address { Street = "100 Vincent Road", Suburb = "St Lukes", City = "Auckland", Country = "New Zealand" }        };        App.customers.Add(customer);        App.customers.Add(new Customer { FirstName = "Terry", LastName = "Teo", Email = "tete@mana.com", Phone = "021 756 382",            Address = new Address { Street = "23 Ford St", City = "Auckland", Suburb = "Pakuranga", Country = "New Zealand" }        });    }
查看完整描述

2 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

您遇到的问题是SetupCustomer,您没有清除列表,因此您的收藏中有多个条目。


private void SetupCustomer()

{

    App.customers.Clear();

    var customer = new Customer { FirstName = "Timothy", LastName = "Jennings", Email = "tim.jennings@gmail.com", Phone = "0275 202020",

     Address = new Address { Street = "100 Burt Road", Suburb = "Howick", City = "Auckland", Country = "New Zealand" }

        };

     App.customers.Add(customer);

     customer = new Customer { FirstName = "Brian", LastName = "Jones", Email = "bjones@gmail.com", Phone = "0275 903070",

     Address = new Address { Street = "100 Vincent Road", Suburb = "St Lukes", City = "Auckland", Country = "New Zealand" }

        };

     App.customers.Add(customer);

     App.customers.Add(new Customer { FirstName = "Terry", LastName = "Teo", Email = "tete@mana.com", Phone = "021 756 382",

     Address = new Address { Street = "23 Ford St", City = "Auckland", Suburb = "Pakuranga", Country = "New Zealand" }

     });


}


查看完整回答
反对 回复 2021-10-24
?
慕娘9325324

TA贡献1783条经验 获得超4个赞

在您SetupCustomer的课堂方法中,AddCustomerUI在开始时添加

 App.customers.Clear();

这应该有助于您的重复数据。


查看完整回答
反对 回复 2021-10-24
  • 2 回答
  • 0 关注
  • 204 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信