当前位置:   article > 正文

WPF使用ObservableCollection<T>来绑定ItemControl类容器 例子_itemsource 绑定 observablecollection,add

itemsource 绑定 observablecollection,add

 

一.新建Model,User.cs类,继承INPC接口

  1. public class User : INotifyPropertyChanged
  2. {
  3. public event PropertyChangedEventHandler PropertyChanged;
  4. private string _age;
  5. public string Age
  6. {
  7. get { return _age; }
  8. set
  9. {
  10. _age = value;
  11. if (PropertyChanged != null)
  12. {
  13. PropertyChanged(this, new PropertyChangedEventArgs("Age"));
  14. }
  15. }
  16. }
  17. private string _name;
  18. public string Name
  19. {
  20. get { return _name; }
  21. set
  22. {
  23. _name = value;
  24. if (PropertyChanged != null)
  25. {
  26. PropertyChanged(this, new PropertyChangedEventArgs("Name"));
  27. }
  28. }
  29. }
  30. private string _sex;
  31. public string Sex
  32. {
  33. get { return _sex; }
  34. set
  35. {
  36. _sex = value;
  37. if (PropertyChanged != null)
  38. {
  39. PropertyChanged(this, new PropertyChangedEventArgs("Sex"));
  40. }
  41. }
  42. }
  43. }

 

二.XAML代码

  1. <Window x:Class="ObservableCollectionDemo.MainWindow"
  2. Title="MainWindow" Height="450" Width="800">
  3. <Grid>
  4. <StackPanel>
  5. <DataGrid Name="dgUser" Height="200" MouseLeftButtonUp="dgStudent_MouseLeftButtonUp"></DataGrid>
  6. <StackPanel >
  7. <TextBox Name="txtName" ></TextBox>
  8. <TextBox Name="txtAge" ></TextBox>
  9. <TextBox Name="txtSex" ></TextBox>
  10. <Button Name="btnAdd" Content="添加" Click="btnAdd_Click"></Button>
  11. <Button Name="btnUpdate" Content="编辑" Click="btnUpdate_Click"></Button>
  12. <Button Name="btnDelete" Content="删除" Click="btnDelete_Click"></Button>
  13. </StackPanel>
  14. </StackPanel>
  15. </Grid>
  16. </Window>

 

三.后台代码:

  1. namespace ObservableCollectionDemo
  2. {
  3. /// <summary>
  4. /// MainWindow.xaml 的交互逻辑
  5. /// </summary>
  6. public partial class MainWindow : Window
  7. {
  8. public MainWindow()
  9. {
  10. InitializeComponent();
  11. Loaded += Window_Loaded;
  12. }
  13. public ObservableCollection<User> datalist = new ObservableCollection<User>();
  14. private void Window_Loaded(object sender, RoutedEventArgs e)
  15. {
  16. dgUser.ItemsSource = datalist;//绑定ItemSource,使用ObervableCollection
  17. }
  18. private void btnAdd_Click(object sender, RoutedEventArgs e)
  19. {
  20. User user = new User();
  21. user.Name = txtName.Text;
  22. user.Age = txtAge.Text;
  23. user.Sex = txtSex.Text;
  24. datalist.Add(user);
  25. }
  26. private void btnUpdate_Click(object sender, RoutedEventArgs e)
  27. {
  28. foreach (var item in datalist)
  29. {
  30. if (item.Name == txtName.Text)
  31. {
  32. item.Age = txtAge.Text;
  33. item.Sex = txtSex.Text;
  34. }
  35. }
  36. }
  37. private void btnDelete_Click(object sender, RoutedEventArgs e)
  38. {
  39. var ca = (User)this.dgUser.SelectedItem;
  40. datalist.Remove(ca);
  41. }
  42. //单机选中一行,修改下方编辑窗的内容
  43. private void dgStudent_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  44. {
  45. var selectUser = (User)this.dgUser.SelectedItem;
  46. if (selectUser != null)
  47. {
  48. txtName.Text = selectUser.Name;
  49. txtAge.Text = selectUser.Age;
  50. txtSex.Text = selectUser.Sex;
  51. }
  52. }
  53. }
  54. }

 

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号