新建WPF项目:
HeroBll.cs代码:
1 public class HeroBll 2 { 3 public ObservableCollection<HeroViewModel> BindViewList { get; set; } 4 public HeroViewModel heroViewModel { get; set; } 5 public HeroBll() 6 { 7 BindViewList = new ObservableCollection<HeroViewModel>(); 8 heroViewModel = new HeroViewModel(); 9 } 10 public void OperationClick(HeroOperType operType) 11 { 12 switch (operType) 13 { 14 case HeroOperType.Query: 15 List<HeroModel> ls = new List<HeroModel>() 16 { 17 new HeroModel("诺克萨斯之手 - 德莱厄斯", "男", "上", "诺克萨斯断头台"), 18 new HeroModel("蛮族之王 - 泰达米尔", "男", "上/野", "无尽怒火"), 19 new HeroModel("武器大师 - 贾克斯", "男", "上/野", "宗师之威"), 20 new HeroModel("无极剑圣 - 易大师", "男", "上/中/野", "高原血统"), 21 new HeroModel("麦林炮手 - 崔丝塔娜", "女", "ADC", "毁灭射击") 22 }; 23 foreach (var item in ls) 24 { 25 HeroViewModel vm = new HeroViewModel(); 26 vm.Main = item; 27 BindViewList.Add(vm); 28 } 29 break; 30 case HeroOperType.Add: 31 HeroViewModel addVM = new HeroViewModel(); 32 addVM.Main = new HeroModel("荒漠屠夫 - 雷克顿", "男", "上", "终极统治"); 33 BindViewList.Add(addVM); 34 break; 35 case HeroOperType.Edit: 36 if (BindViewList.Count > 1) 37 BindViewList[1] = BindViewList[2]; 38 break; 39 case HeroOperType.Delete: 40 if (BindViewList.Count > 1) 41 BindViewList.Remove(BindViewList[1]); 42 break; 43 default: 44 break; 45 } 46 } 47 }
HeroModel.cs代码:
1 public class HeroModel 2 { 3 public string Name { get; set; } 4 public string Sex{ get ; set; } 5 6 public string Position{ get; set; } 7 8 public string Skill { get; set; } 9 10 public HeroModel() 11 { 12 } 13 public HeroModel(string name, string sex, string position, string skill) 14 { 15 this.Name = name; 16 this.Sex = sex; 17 this.Position = position; 18 this.Skill = skill; 19 } 20 }
HeroOperType.cs枚举:
1 public enum HeroOperType 2 { 3 Query, 4 Add, 5 Edit, 6 Delete 7 }
HeroViewModel.cs代码:
1 public class HeroViewModel : INotifyPropertyChanged 2 { 3 public event PropertyChangedEventHandler PropertyChanged; 4 private HeroModel main; 5 public HeroModel Main 6 { 7 get { return main; } 8 set 9 { 10 main = value; 11 if (PropertyChanged != null) 12 PropertyChanged(this, new PropertyChangedEventArgs("Main")); 13 } 14 } 15 16 public HeroViewModel() 17 { 18 Main = new HeroModel(); 19 } 20 }
MainWindow.xaml代码:
1 <Window x:Class="TestDataGrid.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="英雄信息" Height="400" Width="600" WindowStartupLocation="CenterScreen" Icon="lol.ico"> 5 <Window.Resources> 6 <Style TargetType="Button"> 7 <Setter Property="Width" Value="60"/> 8 <Setter Property="Background" Value="White"/> 9 </Style> 10 <Style TargetType="DataGridRow"> 11 <Setter Property="Background" Value="#F2F2F2"/> 12 <Setter Property="Height" Value="30"/> 13 <Setter Property="Foreground" Value="Black"/> 14 <Style.Triggers> 15 <Trigger Property="AlternationIndex" Value="0"> 16 <Setter Property="Background" Value="#F5FFFA" /> 17 </Trigger> 18 <Trigger Property="AlternationIndex" Value="1"> 19 <Setter Property="Background" Value="#D1EEEE" /> 20 </Trigger> 21 <Trigger Property="IsMouseOver" Value="True"> 22 <Setter Property="Background" Value="LightGray"/> 23 </Trigger> 24 <Trigger Property="IsSelected" Value="True"> 25 <Setter Property="Background" Value="#00BFFF"/> 26 </Trigger> 27 </Style.Triggers> 28 </Style> 29 <Style TargetType="DataGrid"> 30 <Setter Property="Background" Value="#E0EEEE"/> 31 <Setter Property="BorderBrush" Value="#d6c79b"/> 32 <Setter Property="AlternationCount" Value="2"/> 33 <Setter Property="HorizontalGridLinesBrush"> 34 <Setter.Value> 35 <SolidColorBrush Color="#d6c79b"/> 36 </Setter.Value> 37 </Setter> 38 <Setter Property="VerticalGridLinesBrush"> 39 <Setter.Value> 40 <SolidColorBrush Color="#d6c79b"/> 41 </Setter.Value> 42 </Setter> 43 </Style> 44 <Style TargetType="DataGridColumnHeader"> 45 <Setter Property="SnapsToDevicePixels" Value="True"/> 46 <Setter Property="MinWidth" Value="0"/> 47 <Setter Property="MinHeight" Value="30"/> 48 <Setter Property="Foreground" Value="#323433"/> 49 <Setter Property="HorizontalContentAlignment" Value="Center"/> 50 <Setter Property="VerticalContentAlignment" Value="Center"/> 51 <Setter Property="FontSize" Value="15"/> 52 <Setter Property="FontWeight" Value="Bold"/> 53 <Setter Property="Cursor" Value="Hand"/> 54 </Style> 55 <Style TargetType="DataGridCell"> 56 <Setter Property="Template"> 57 <Setter.Value> 58 <ControlTemplate TargetType="DataGridCell"> 59 <TextBlock TextAlignment="Center" VerticalAlignment="Center"> 60 <ContentPresenter/> 61 </TextBlock> 62 </ControlTemplate> 63 </Setter.Value> 64 </Setter> 65 <Setter Property="FontSize" Value="15"/> 66 </Style> 67 </Window.Resources> 68 <Grid> 69 <Grid.RowDefinitions> 70 <RowDefinition Height="*"></RowDefinition> 71 <RowDefinition Height="30"></RowDefinition> 72 </Grid.RowDefinitions> 73 <DataGrid Grid.Row="0" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" IsReadOnly="True" ItemsSource="{Binding BindViewList}"> 74 <DataGrid.Columns> 75 <DataGridTextColumn Header="姓名" Width="2*" Binding="{Binding Main.Name}"></DataGridTextColumn> 76 <DataGridTextColumn Header="性别" Width="*" Binding="{Binding Main.Sex}"></DataGridTextColumn> 77 <DataGridTextColumn Header="位置" Width="*" Binding="{Binding Main.Position}"></DataGridTextColumn> 78 <DataGridTextColumn Header="技能" Width="2*" Binding="{Binding Main.Skill}"></DataGridTextColumn> 79 </DataGrid.Columns> 80 </DataGrid> 81 <StackPanel Grid.Row="1" Height="25" Orientation="Horizontal" HorizontalAlignment="Right"> 82 <Button CommandParameter="Add" Click="Button_Click">添加</Button> 83 <Button CommandParameter="Edit" Click="Button_Click">修改</Button> 84 <Button CommandParameter="Delete" Click="Button_Click">删除</Button> 85 <Button CommandParameter="Query" Click="Button_Click">查询</Button> 86 </StackPanel> 87 </Grid> 88 </Window>
MainWindow.xaml.cs调用:
1 public partial class MainWindow : Window 2 { 3 private HeroBll heroBll { get; set; } 4 public MainWindow() 5 { 6 InitializeComponent(); 7 heroBll = new HeroBll(); 8 this.DataContext = heroBll; 9 } 10 11 private void Button_Click(object sender, RoutedEventArgs e) 12 { 13 var param = (sender as Button).CommandParameter.ToString(); 14 HeroOperType operType = (HeroOperType)Enum.Parse(typeof(HeroOperType), param); 15 heroBll.OperationClick(operType); 16 } 17 }
效果图: