三层架构ListUI和EditUI以及数据绑定(2)-18

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

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

设计入下:

代码如下:

<Window x:Class="ExecuteReader执行查询.CustomerListUI"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CustomerListUI" Height="300" Width="800" WindowState="Maximized" Loaded="Window_Loaded">
    <Grid Name="gridCustomers">
        <DockPanel Height="189" HorizontalAlignment="Left" Margin="0,9,0,0" Name="dockPanel1" VerticalAlignment="Top" >
            <ToolBar DockPanel.Dock="Top" Height="30" Name="toolBar1" Width="200" >
                <Button Name="btnAdd" Click="btnAdd_Click">
                    <Image Source="image/add.ico" ></Image>
                </Button>
                <Button Name="btnDelete" Click="btnDelete_Click">
                    <Image Source="image/delete.ico"></Image>
                </Button>
                <Button Name="btnEdit" Click="btnEdit_Click">
                    <Image Source="image/edit.ico" ></Image>
                </Button>  
            </ToolBar>
            <DataGrid DockPanel.Dock="Top" IsReadOnly="True" Name="dgCustomers" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="姓名" Binding="{Binding Name}" Width="100"></DataGridTextColumn>
                    <DataGridTextColumn Header="生日" Binding="{Binding Birthday}" Width="100"></DataGridTextColumn>
                    <DataGridTextColumn Header="电话" Binding="{Binding TelNum}" Width="100"></DataGridTextColumn>
                    <DataGridTextColumn Header="地址" Binding="{Binding Address}" Width="150"></DataGridTextColumn>
                    <DataGridTextColumn Header="等级" Binding="{Binding CustLevel}" Width="100"></DataGridTextColumn>
                </DataGrid.Columns>


            </DataGrid>
        </DockPanel>
    </Grid>
</Window>

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执行查询
{
    /// <summary>
    /// CustomerListUI.xaml 的交互逻辑
    /// </summary>
    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,设计如下:

代码如下:

<Window x:Class="ExecuteReader执行查询.CustomerEditUI"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CustomerEditUI" Height="250" Width="500" Loaded="Window_Loaded">
    <Grid Name="gridEditUI">
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="26,22,0,0" Name="tbName" Text="姓名" VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="243,18,0,0" Name="tbTelNum" Text="电话" VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="25,64,0,0" Name="tbBirthDay" Text="生日" VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="252,70,0,0" Name="tbCustLevel" Text="等级" VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="26,104,0,0" Name="tbAddress" Text="通讯地址" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="74,18,0,0" Name="txtName"  Text="{Binding Name}" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="285,15,0,0" Name="txtTelNum" Text="{Binding TelNum}" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="285,61,0,0" Name="txtCustomerLevel"  Text="{Binding CustLevel}" VerticalAlignment="Top" Width="120" />
        <TextBox Height="26" HorizontalAlignment="Left" Margin="80,101,0,0" Name="txtAddress"  Text="{Binding Address}" VerticalAlignment="Top" Width="378" />
        <Button Content="保存" Height="23" HorizontalAlignment="Left" Margin="285,162,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" Click="btnSave_Click" />
        <Button Content="取消" Height="23" HorizontalAlignment="Left" Margin="391,162,0,0" Name="btnCancel" VerticalAlignment="Top" Width="75" Click="btnCancel_Click" />
        <DatePicker  SelectedDate="{Binding BirthDay}" Height="25" HorizontalAlignment="Left" Margin="78,60,0,0" Name="dpBirthDay" VerticalAlignment="Top" Width="115" />
    </Grid>
</Window>

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执行查询
{    /// <summary>
    /// CustomerEditUI.xaml 的交互逻辑
    /// </summary>
    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;
            }
        }
    }
}

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

 

posted @ 2013-06-07 10:52  秋水惜朝  阅读(419)  评论(0编辑  收藏  举报