博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
三层架构ListUI和EditUI以及数据绑定(2)-18
阅读量:5160 次
发布时间:2019-06-13

本文共 6641 字,大约阅读时间需要 22 分钟。

 第一步,创建一个文件夹: image  添加三个图标图像,分别为: add.ico,delete.ico,edit.ico

第二步:新建一个窗体为:CustomerListUI.xaml

设计入下:

代码如下:

CustomerListUI.xaml.cs的代码如下:

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.Shapes;using ExecuteReader执行查询.DAL;using ExecuteReader执行查询.Model;namespace ExecuteReader执行查询{    ///     /// CustomerListUI.xaml 的交互逻辑    ///     public partial class CustomerListUI : Window    {        public CustomerListUI()        {            InitializeComponent();        }        private void LoadData()        {            CustomerDAL dal = new CustomerDAL();            dgCustomers.ItemsSource = dal.GetAll();        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            //CustomerDAL dal = new CustomerDAL();            //dgCustomers.ItemsSource=dal.GetAll();            LoadData();        }        private void btnAdd_Click(object sender, RoutedEventArgs e)        {            CustomerEditUI editUI = new CustomerEditUI();            editUI.IsInsert=true;            if(editUI.ShowDialog()==true)            {                LoadData();            }        }        private void btnDelete_Click(object sender, RoutedEventArgs e)        {            Customer customer = (Customer)dgCustomers.SelectedItem;            if (customer == null)            {                MessageBox.Show("请选择要删除的行!");                return;            }            if (MessageBox.Show("确认删除此条数据吗?", "提醒", MessageBoxButton.YesNo) == MessageBoxResult.Yes)            {                new CustomerDAL().DeleteById(customer.Id);                //再次刷新数据,因为与Window_Loaded()事件代码相同,重复使用,所以将此代码封装成为LoadData()方法                //CustomerDAL dal = new CustomerDAL();                //dgCustomers.ItemsSource = dal.GetAll();                LoadData();            }        }        private void btnEdit_Click(object sender, RoutedEventArgs e)        {            Customer customer =(Customer)dgCustomers.SelectedItem;            if (customer == null)            {                MessageBox.Show("请选择要编辑的行");                return;            }            CustomerEditUI editUI = new CustomerEditUI();            editUI.IsInsert = false;            editUI.EditingId = customer.Id;            if (editUI.ShowDialog() == true)            {                LoadData();            }        }          }}

第三步:新建一个窗体为:CustomerEditUI.xaml,设计如下:

代码如下:

CustomerEditUI.xaml .cs的代码如下:

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.Shapes;using ExecuteReader执行查询.Model;using ExecuteReader执行查询.DAL;namespace ExecuteReader执行查询{    ///     /// CustomerEditUI.xaml 的交互逻辑    ///     public partial class CustomerEditUI : Window    {  //判断是新增数据还是修改数据,因为新增和修改时候打开的都是编辑这一页面        public bool IsInsert { get; set; }        //如果是编辑的话,需要得到编辑行的Id        public long EditingId { get; set; }        public CustomerEditUI()        {            InitializeComponent();        }        private void btnSave_Click(object sender, RoutedEventArgs e)        {            if (IsInsert)            {
//用户修改过程中会自动将界面的修改同步到Customer对象中。 Customer customer = (Customer)gridEditUI.DataContext;//因为在窗体加载时候已经创建了一个对象,现在只是接收它即可。 //gridEditUI.DataContext指向的Customer对象。 //Customer customer = new Customer(); //customer.Name = txtName.Text; //customer.TelNum = txtTelNum.Text; //customer.Address = txtAddress.Text; //customer.CustLevel = Convert.ToInt32(txtCustomerLevel.Text); //customer.BirthDay = dpBirthDay.SelectedDate; //插入数据库 new CustomerDAL().Insert(customer); } else//先从数据库中查询旧的值,然后把界面上的值设置到旧对象上,Update更新。 { Customer customer = (Customer)gridEditUI.DataContext; CustomerDAL dal = new CustomerDAL(); //Customer customer=dal.GetById(EditingId); //customer.Name = txtName.Text; //customer.TelNum = txtTelNum.Text; //customer.Address = txtAddress.Text; //customer.CustLevel = Convert.ToInt32(txtCustomerLevel.Text); //customer.BirthDay = dpBirthDay.SelectedDate; //更新 dal.Update(customer); } DialogResult = true; } private void btnCancel_Click(object sender, RoutedEventArgs e) { DialogResult = false; } private void Window_Loaded(object sender, RoutedEventArgs e) { if (IsInsert) { //txtCustomerLevel.Text = "2";//默认为2级用户 Customer cust=new Customer(); cust.CustLevel = 2; gridEditUI.DataContext = cust; } else//编辑修改,将数据从数据库中拿出来,填充到页面上 { //可以把Customer直接在ListUI中传过来,这样还省得查一次数据库。 //但是一个原则:在窗口传值和容器中存储值,尽量传简单数据类型。 Customer customer = new CustomerDAL().GetById(EditingId); //txtName.Text = customer.Name; //txtTelNum.Text = customer.TelNum; //txtCustomerLevel.Text = customer.CustLevel.ToString(); //txtAddress.Text = customer.Address; //dpBirthDay.SelectedDate = customer.BirthDay; gridEditUI.DataContext = customer; } } }}

注意:其中涉及一些数据绑定的知识。

 

转载于:https://www.cnblogs.com/qiushuixizhao/archive/2013/06/07/3123422.html

你可能感兴趣的文章
如何将应用完美迁移至Android P版本
查看>>
【转】清空mysql一个库中的所有表的数据
查看>>
基于wxPython的python代码统计工具
查看>>
淘宝JAVA中间件Diamond详解(一)---简介&快速使用
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>
Kettle学习系列之Kettle能做什么?(三)
查看>>
Day03:Selenium,BeautifulSoup4
查看>>
awk变量
查看>>
mysql_对于DQL 的简单举例
查看>>
35. Search Insert Position(C++)
查看>>
[毕业生的商业软件开发之路]C#异常处理
查看>>
一些php文件函数
查看>>
有关快速幂取模
查看>>
Linux运维必备工具
查看>>
字符串的查找删除
查看>>
NOI2018垫底记
查看>>
快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
查看>>
Codeforces Round #277 (Div. 2)
查看>>
【更新】智能手机批量添加联系人
查看>>
NYOJ-128前缀式计算
查看>>