How to group procedures and reuse - ViewModel

jennyliu835 221 Reputation points
2020-09-15T17:26:21.81+00:00

Hi Peter,
How to group procedures on line 4, 5 and 6 into one method and reuse it at its original place (line 4, or 5 or 6 ) and on line 26.
Thanks

  1. class Pages_ViewModel : ICommand, INotifyPropertyChanged
  2. {
  3. public Color Tb3BackgroundColor { get; set; } = Colors.Orange;
  4. public Visibility Tb1Visibility { get; set; } = Visibility.Hidden;
  5. public Visibility Tb2Visibility { get; set; } = Visibility.Hidden;
  6. public Visibility Tb3Visibility { get; set; } = Visibility.Hidden;
  7. public bool Tb3BeginStoryBoard { get; set; } = false;
    1. public void Execute(object parameter)
  8. {
  9. switch (parameter.ToString())
  10. {
  11. case "General":
  12. Tb1Visibility = Visibility.Visible;
  13. OnPropertyChanged(nameof(Tb1Visibility));
  14. break;
    1. case "Primary":
  15. Tb2Visibility = Visibility.Visible;
  16. Tb3Visibility = Visibility.Visible;
  17. OnPropertyChanged(nameof(Tb2Visibility));
  18. OnPropertyChanged(nameof(Tb3Visibility));
  19. break;
    1. case "Secondary":
  20. //TbCommonForeGround = new SolidColorBrush(Colors.Blue);
  21. Tb3BeginStoryBoard = true;
  22. OnPropertyChanged(nameof(Tb3BeginStoryBoard));
  23. break;
    1. default:
  24. break;
  25. }
  26. }
  27. .....
    1. }
  28. 39.
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,697 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-09-16T17:53:23.05+00:00

    Hi Jenny,
    try this tested code:

    using System;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    
    namespace WpfApp2.general_pages
    {
      class Pages_ViewModel : ICommand, INotifyPropertyChanged
      {
        public Color Tb3BackgroundColor { get; set; } = Colors.Orange;
        public Visibility Tb1Visibility { get; set; } = Visibility.Hidden;
        public Visibility Tb2Visibility { get; set; } = Visibility.Hidden;
        public Visibility Tb3Visibility { get; set; } = Visibility.Hidden;
        public bool Tb3BeginStoryBoard { get; set; } = false;
    
        private void SetVisibility(bool? tb1, bool? tb2, bool? tb3)
        {
          if (tb1.HasValue)
          {
            this.Tb1Visibility = (tb1.Value) ? Visibility.Visible : Visibility.Hidden;
            OnPropertyChanged(nameof(Tb1Visibility));
          }
          if (tb2.HasValue)
          {
            this.Tb2Visibility = (tb2.Value) ? Visibility.Visible : Visibility.Hidden;
            OnPropertyChanged(nameof(Tb2Visibility));
          }
          if (tb3.HasValue)
          {
            this.Tb3Visibility = (tb3.Value) ? Visibility.Visible : Visibility.Hidden;
            OnPropertyChanged(nameof(Tb3Visibility));
          }
        }
    
        public void Execute(object parameter)
        {
          switch (parameter.ToString())
          {
            case "General":
              SetVisibility(true, null, null);
              break;
    
            case "Primary":
              SetVisibility(null, true, true);
              break;
    
            case "Secondary":
              Tb3BeginStoryBoard = true;
              OnPropertyChanged(nameof(Tb3BeginStoryBoard));
              break;
    
            default:
              break;
          }
        }
    
        public event EventHandler CanExecuteChanged;
        public bool CanExecute(object parameter) => true;
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propName = "") =>
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
      }
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful