c# - 重新加载页面时出现数据网格错误
问题描述
我有一个添加客户 UI,它应用了一个数据网格来显示提供的条目,但是如果我在已经显示的同时重新选择添加客户,它将再次添加条目。
第 1-3 行是原始的,第 4-6 行是重复的。有谁知道如何限制这种情况发生?
在我的 MainActivity 中,我有一个带有框架的菜单导航来填充 xaml 页面。下面是初始化类的代码。
private void AddCustomer_Click(object sender, RoutedEventArgs e)
{
Main.Content = new AddCustomerUI();
}
Frame 称为 Main,然后将 xaml 页面分配给它的 Main.content
public 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" }
});
}
private void SaveCustomer_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(fNameTxt.Text) || string.IsNullOrWhiteSpace(lNameTxt.Text) || string.IsNullOrWhiteSpace(emailTxt.Text)
|| string.IsNullOrWhiteSpace(phTxt.Text))
//MessageBox.Show("Empty Fields", "Please look at filling out all fields on the form.");
return;
var customer = new Customer
{
FirstName = fNameTxt.Text, LastName = lNameTxt.Text, Email = emailTxt.Text, Phone = phTxt.Text,
Address = new Address { Street = streetTxt.Text, Suburb = suburbTxt.Text, City = cityTxt.Text, Country = countryTxt.Text }
};
App.customers.Add(customer);
customerDataGrid.Items.Refresh();
ResetText();
}
private void ResetText()
{
fNameTxt.Text = string.Empty; lNameTxt.Text = string.Empty;
emailTxt.Text = string.Empty; phTxt.Text = string.Empty;
streetTxt.Text = string.Empty; cityTxt.Text = string.Empty;
suburbTxt.Text = string.Empty; countryTxt.Text = string.Empty;
}
}
解决方案
在您SetupCustomer
的课程方法中,AddCustomerUI
在开始时添加
App.customers.Clear();
这应该有助于您处理重复的数据。
推荐阅读
- php - 语法错误,PHP 脚本中出现意外的“endforeach”(T_ENDFOREACH)
- javascript - 我正在尝试制作一个 if 语句,对我的对象数组中的不同类型的值进行排序
- python - python3字符串比较像'9'<'10'
- python - 无法在 Python 中跨多个对象使用相同的 SQLite 连接
- javascript - 我怎样才能优化这个javascript代码(电子表格getelementid)
- python - 模块“tensorflow.python.keras.utils.layer_utils”没有属性“cached_per_instance”
- next.js - 为什么我不能使用 QueryCache 从 queryClient.prefetchQuery 获取“缓存”
- git - 如何检查git下项目的所有变化?
- java - 我在一个布局中创建了两个回收器视图,并且我有 nullPointerException
- python - 当熊猫中的值为空时,to_list 不适用于熊猫