关于C#的数据绑定,存取数据库实例详解 (二)
我首先实现了显示的功能(不用去读数据库,将数据直接填到集合里面,通过数据绑定,在界面能都显示出来),所以我应该是从Model和View层开始的。
首先是MainWindow.xaml 界面,代码和界面如下。
有5处关于数据绑定的:
1)ListBox 绑定 InlineTools(这是tool的集合,从数据库中获取所有的tools)但显示的只是Name。
2)SelectedItem绑定的是CurrentInlineTool,当选中其中一条tool时,CurrentInlineTool就是你当前选中的tool。
3) TextBox绑定的是CreateNewTool.Name,当创建一个新的tool时,其Name可以通过用户输入的方式命名。
4)两个Buttton分别绑定CreateCommand和DeleteCommand。新建一个tool以及删除一个tool.
1 <Grid Margin="10"> 2 <TabControl> 3 <TabItem Header="Equipment"> 4 <Grid > 5 <Grid.ColumnDefinitions > 6 <ColumnDefinition Width="220"></ColumnDefinition> 7 <ColumnDefinition Width="*"> 8 </ColumnDefinition> 9 </Grid.ColumnDefinitions> 10 <Grid.RowDefinitions > 11 <RowDefinition Height="40"></RowDefinition> 12 <RowDefinition Height="*"></RowDefinition> 13 <RowDefinition Height="30"></RowDefinition> 14 </Grid.RowDefinitions> 15 <ListView Margin="5" Grid.RowSpan="2" ItemsSource="{Binding InlineTools}" SelectedItem="{Binding CurrentInlineTool,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged }" DisplayMemberPath="Name" ></ListView> 16 <WrapPanel Grid.Column="0" Grid.Row="2" > 17 <!--<Border Height="20" Margin="4,0,0,0" Width="80" BorderBrush="Black" BorderThickness="1"></Border>--> 18 <TextBox Text="{Binding CreateNewTool.Name,Mode=TwoWay }" Height="30" Width="80" ></TextBox> 19 <Button Margin="6,0,0,0" Content="Create" Height="30" Width="60" Command="{Binding CreateCommand}"></Button> 20 <Button Margin="6,0,0,0" Content="Delete" Height="30" Width="60" Command="{Binding DeleteCommand}"></Button> 21 </WrapPanel> 22 </Grid> 23 </TabItem> 24 <TabItem Header="Dispatching"> 25 </TabItem> 26 </TabControl> 27 </Grid>
InlineToolModel.cs代码如下,开始写的时候只写了前两个字段用来创建数据库,后面的方法是用到的时候添加上去的。
1 using CIS.DAL; 2 using Microsoft.Practices.Prism.Mvvm; 3 using SQL.DAL.Model; 4 using SQL.DAL.Repository; 5 using SQLtest.Model; 6 using System; 7 using System.Collections.Generic; 8 using System.Linq; 9 using System.Text; 10 using System.Threading.Tasks; 11 12 namespace SQLtest.Model 13 { 14 public class InlineToolModel:BindableBase, IEntity 15 { 16 public int Id 17 { 18 get; 19 set; 20 } 21 private string name; 22 public string Name 23 { 24 get { return name; } 25 set 26 { 27 if (name != value) 28 { 29 name = value; 30 this.OnPropertyChanged("name"); 31 } 32 } 33 } 34 35 public void SaveTool() 36 { 37 if (string.IsNullOrEmpty(this.Name)) 38 { 39 throw new Exception("用户名不能为空!"); 40 } 41 if (this.IsExistSameName()) 42 { 43 throw new Exception("已存在同名用户!"); 44 } 45 InlineToolModel tool= new InlineToolModel(); 46 tool.Name = this.Name; 47 CISDbContext db = new CISDbContext(); 48 db.t_InlineToolModel.Add(tool); 49 db.SaveChanges(); 50 } 51 public void DeleteTool() 52 { 53 InlineToolModel tool = new InlineToolModel(); 54 tool.Name = this.Name; 55 CISDbContext db = new CISDbContext(); 56 var dblog = db.t_InlineToolModel.FirstOrDefault(d => d.Name == tool.Name); 57 58 if (dblog != null) 59 { 60 db.t_InlineToolModel.Remove(dblog); 61 db.SaveChanges(); 62 } 63 } 64 public bool IsExistSameName() 65 { 66 CISDbContext db = new CISDbContext(); 67 Repository<InlineToolModel> oRep = new Repository<InlineToolModel>(db); 68 var exists = oRep.GetAll().Where(d => d.Name == this.Name && d.Id != this.Id); 69 return exists.Count() > 0; 70 } 71 public InlineToolModel() 72 { 73 } 74 } 75 }
MainWindowViewModel.cs代码如下,在此一定要注意实例化,不然总是会出错。里面定义了InlineTools,CurrentInlineTool ,CreateNewTool,CreateCommand和DeleteCommand,与数据绑定有关。
1 using CIS.DAL; 2 using Microsoft.Practices.Prism.Commands; 3 using Microsoft.Practices.Prism.Mvvm; 4 using SQL.DAL.Repository; 5 using SQLtest.Model; 6 using System; 7 using System.Collections.Generic; 8 using System.Collections.ObjectModel; 9 using System.Data.Entity; 10 using System.Data.Entity.Validation; 11 using System.Linq; 12 using System.Text; 13 14 using System.Threading.Tasks; 15 using System.Windows; 16 17 namespace SQLtest.Model 18 { 19 public class MainWindowViewModel : BindableBase 20 { 21 private ObservableCollection<InlineToolModel> inlineTools; 22 public ObservableCollection<InlineToolModel> InlineTools 23 { 24 get { return inlineTools; } 25 set 26 { 27 inlineTools = value; 28 this.OnPropertyChanged(() => InlineTools); 29 } 30 } 31 private InlineToolModel currentTool; 32 public InlineToolModel CurrentInlineTool 33 { 34 get { return currentTool; } 35 set 36 { 37 currentTool = value; 38 this.OnPropertyChanged(() => CurrentInlineTool); 39 } 40 } 41 private InlineToolModel createNewTool; 42 public InlineToolModel CreateNewTool 43 { 44 get { return createNewTool; } 45 set 46 { 47 createNewTool = value; 48 this.OnPropertyChanged(() => CreateNewTool); 49 } 50 } 51 52 public static DelegateCommand<object> CreateCommand { get; set; } 53 public static DelegateCommand<object> DeleteCommand { get; set; } 54 public MainWindowViewModel() 55 { 56 this.DataRefresh(); 57 CreateCommand = new DelegateCommand<object>(OnCreate); 58 DeleteCommand = new DelegateCommand<object>(OnDelete); 59 CreateNewTool= new Model.InlineToolModel(); 60 61 } 62 63 public void OnCreate(object e) 64 { 65 try 66 { 67 CreateNewTool.SaveTool();//检查有无重名和空,CreateNewTool.SaveTest(CreateNewTool.Name) 68 this.DataRefresh(); 69 } 70 catch (System.Exception ex) 71 { 72 MessageBox.Show(string.Format("{0}", ex.Message)); 73 } 74 } 75 public void OnDelete(object e) 76 { 77 if (CurrentInlineTool == null) return; 78 if (MessageBox.Show(string.Format("Delete Tool:{0}?", CurrentInlineTool.Name), "Delete", MessageBoxButton.OKCancel) == MessageBoxResult.OK) 79 { 80 CurrentInlineTool.DeleteTool(); 81 } 82 DataRefresh(); 83 } 84 public void DataRefresh() 85 { 86 InlineTools = new ObservableCollection<InlineToolModel>(); 87 CISDbContext db = new CISDbContext(); 88 Repository<InlineToolModel> oRep = new Repository<InlineToolModel>(db); 89 foreach (var item in oRep.GetAll()) 90 { 91 InlineTools.Add(item); 92 } 93 } 94 } 95 }
但愿人长久
千里共婵娟