1.前台Grid定义
2.后台代码处理
HostsHelper _helper = new HostsHelper(); public MainWindow() { InitializeComponent(); //绑定列表 Rebind(); } //添加域名和IP绑定 private void Button_Click(object sender, RoutedEventArgs e) { string ip = txtIp.Text; string hostname = txtHostName.Text; if (string.IsNullOrEmpty(hostname)) { txtHostName.Focus(); ShowResult("请输入域名"); return; } if (string.IsNullOrEmpty(ip)) { txtIp.Focus(); ShowResult("请输入IP地址"); return; } //添加结果 _helper.Add(new HostData(true, ip, hostname)); MessageBox.Show("添加成功"); //重新绑定 Rebind(); } //重新绑定结果 public void Rebind() { gridOne.ItemsSource = _helper.HostDatas; gridOne.Items.Refresh(); } //显示结果 public void ShowResult(string str) { Result.Content = str; } //删除内容 private void Button_Click_1(object sender, RoutedEventArgs e) { Button btn = sender as Button; MessageBoxResult result = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { _helper.Remove(new HostData(true,btn.Uid.ToString(),btn.ContentStringFormat.ToString())); Rebind(); } }