Guest User

Untitled

a guest
Jun 26th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. <Window.DataContext>
  2. <local:MainWindowViewModel/>
  3. </Window.DataContext>
  4. <Grid>
  5. <Grid.ColumnDefinitions>
  6. <ColumnDefinition Width="*"/>
  7. <ColumnDefinition Width="100"/>
  8. </Grid.ColumnDefinitions>
  9. <Button Command="{Binding AddPersonCommand}" Grid.Column="1"/>
  10. <ListBox ItemsSource="{Binding People}"
  11. >
  12. <ListBox.ItemTemplate>
  13. <DataTemplate>
  14. <TextBlock Text="{Binding LastName}"/>
  15. </DataTemplate>
  16. </ListBox.ItemTemplate>
  17. </ListBox>
  18. </Grid>
  19.  
  20. public class MainWindowViewModel : BaseViewModel
  21. {
  22. private RelayCommand addPersonCommand;
  23. public RelayCommand AddPersonCommand
  24. {
  25. get
  26. {
  27. return addPersonCommand
  28. ?? (addPersonCommand = new RelayCommand(
  29. () =>
  30. {
  31. People.Add(new Person { FirstName = "Adam", LastName = "Barlow" });
  32. }
  33. ));
  34. }
  35. }
  36. private ObservableCollection<Person> people = new ObservableCollection<Person>();
  37.  
  38. public ObservableCollection<Person> People
  39. {
  40. get { return people; }
  41. set { people = value; }
  42. }
  43.  
  44.  
  45. public ListCollectionView LCV { get; set; }
  46. public MainWindowViewModel()
  47. {
  48. LCV = (ListCollectionView)CollectionViewSource.GetDefaultView(People);
  49. LCV.SortDescriptions.Add(
  50. new SortDescription("LastName", ListSortDirection.Ascending));
  51. People.Add(new Person { FirstName = "Chesney", LastName = "Brown" });
  52. People.Add(new Person { FirstName = "Gary", LastName = "Windass" });
  53. People.Add(new Person { FirstName = "Liz", LastName = "McDonald" });
  54. People.Add(new Person { FirstName = "Carla", LastName = "Connor" });
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment