Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. public sealed class DelegateCommand<T> : Command
  2. {
  3.     private readonly Action<T> execute;
  4.     private readonly Func<T, bool> canExecute;
  5.  
  6.     public DelegateCommand(Action<T> execute, Func<T, bool> canExecute)
  7.     {
  8.         this.execute = execute;
  9.         this.canExecute = canExecute;
  10.     }
  11.  
  12.     public override bool CanExecute(object parameter)
  13.     {
  14.         return (canExecute != null) ? canExecute((T)parameter) : true;
  15.     }
  16.    
  17.     public override void Execute(object parameter)
  18.     {
  19.         execute((T)parameter);
  20.     }
  21. }
  22.  
  23.  
  24. public sealed class MainWindowViewModel : INotifyPropertyChanged {
  25.     private string city;
  26.  
  27.     public MainWindowViewModel(... service) {
  28.         this.service = service;
  29.  
  30.         ListCommand = new DelegateCommand<object>(obj => ListRealties, null);
  31.         Realties = new ObservableCollection<Realty>();
  32.     }
  33.  
  34.     public event PropertyChangedEventHandler PropertyChanged;
  35.  
  36.     public string City {
  37.         get { return city; }
  38.         set {
  39.             if (value != city) {
  40.                 city = value;
  41.                 var handler = PropertyChanged;
  42.                 if (handler != null)
  43.                     handler(this, new PropertyChangedEventArgs("City"));
  44.             }
  45.         }
  46.     }
  47.  
  48.     public ObservableCollection<Realty> Realties { get; private set; }
  49.  
  50.     private void ListRealties() {
  51.         Realties.Clear();
  52.         foreach (var realty in service.GetAllProperties(City))
  53.             Realties.Add(new Realty(realty.Condition, realty.Street));
  54.     }
  55. }
  56.  
  57.  
  58.  
  59. <Window>
  60.     <TextBox Text="{Binding City}"/>
  61.     <ListBox ItemsSource="{Binding Realties}"/>
  62.     <Button Command="{Binding ListCommand}" Content="Auflisten"/>
  63. </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement