Change the selected Value of a combo box from a window button

Sep 26 2020 5:57 PM
How do I change the selected Value of a combo box from a window button when combo box is binded with enumList in its own viewmodel?
 
I have two Windows and several user controls in my project, one of the user control Ribbon is placed on MainWindow.
 
The Ribbon is groupBox user control containing a comboBox which is binded with enum PackageType as follows:
  1. namespace ManglamStellaris.ViewModels    
  2. {    
  3.     public class RibbonViewModel : ViewModel    
  4.     {    
  5.         public event Action<PackageType> OnPackageSelection;    
  6.     
  7.         public RibbonViewModel()    
  8.         {    
  9.     
  10.             ListPackageType = new List<Tuple<PackageType, string>>(new Tuple<PackageType, string>[]    
  11.             {    
  12.                    new Tuple<PackageType, string>(PackageType.Raw, "Raw"),    
  13.                    new Tuple<PackageType, string>(PackageType.Lagna, "Lagna"),    
  14.                    new Tuple<PackageType, string>(PackageType.LaalKitab, "LaalKitab")    
  15.     
  16.             });    
  17.     
  18.         }    
  19.             
  20.         public List<Tuple<PackageType, String>> ListPackageType { getprivate set; }    
  21.     
  22.         public PackageType packageType    
  23.         {    
  24.             get { return _packageType; }    
  25.             set    
  26.             {    
  27.                 _packageType = value;    
  28.                 RaisePropertyChanged("packageType");    
  29.     
  30.                 if (OnPackageSelection != null)    
  31.                 {    
  32.                     OnPackageSelection(_packageType);    
  33.                 }    
  34.             }    
  35.         }    
  36.     
  37.         private PackageType _packageType;    
  38.     
  39.     }    
  40. }  
An enum PackageType is defined in enums.cs
  1. public enum PackageType      
  2. {      
  3.     Raw,      
  4.     Lagna,      
  5.     LaalKitab      
  6. }  
RibbonViewModel is initialized in MainViewModel as follows:
  1. public class MainViewModel : ViewModel    
  2. {    
  3.     Dictionary<Type, object> _Services = new Dictionary<Type, object>();    
  4.     public event Action<PackageType> OnPackageSelection;    
  5.     public MainViewModel() {    
  6.           Config = new ConfigViewModel();    
  7.           Input = new InputViewModel();    
  8.           Result = new CalculationResultViewModel();    
  9.           RibbonViewModel = new RibbonViewModel();    
  10.           RibbonViewModel.OnPackageSelection += (PackageType) => {    
  11.               if (OnPackageSelection != null) {    
  12.                   OnPackageSelection(PackageType);    
  13.               }    
  14.         };    
  15.     
  16.           DoCalculationCommand = new RelayCommand(() => {    
  17.               DoCalculation();    
  18.       });    
  19. }  
MainWindow controls the user control shown in the content control area of Main Window based on selection type:
  1. public partial class MainWindow : Window    
  2. {    
  3.     public MainViewModel ViewModel;    
  4.     
  5.     public ResultView ResultView;    
  6.     public LagnaChart LagnaChart;    
  7.     public LaalKitab LaalKitab;    
  8.     
  9.     public MainWindow(MainViewModel mainViewModel)    
  10.     {    
  11.         InitializeComponent();    
  12.         ViewModel = mainViewModel;    
  13.         ViewModel.OnPackageSelection += (PackageType) => {    
  14.             if (PackageType == PackageType.LaalKitab)    
  15.             {    
  16.                 this.mainContentArea.Content = LaalKitab;    
  17.             }    
  18.             else if (PackageType == PackageType.Lagna)    
  19.             {    
  20.                 this.mainContentArea.Content = LagnaChart;    
  21.             }    
  22.             else if (PackageType == PackageType.Raw)    
  23.             {    
  24.                 this.mainContentArea.Content = ResultView;    
  25.             }    
  26.         };    
  27.     
  28.         DataContext = ViewModel;    
  29.     
  30.         ResultView = new ResultView(ViewModel);    
  31.         LagnaChart = new LagnaChart(ViewModel);    
  32.         LaalKitab = new LaalKitab(ViewModel);    
  33.     
  34.         this.mainContentArea.Content = ResultView;    
  35.     }    
  36. }  
How can I select the combo box value from another Window Button Click function which is as follows:
  1. public partial class HomeWindow : Window    
  2. {    
  3.     public LagnaChart LagnaChart;    
  4.     MainViewModel _mainViewModel = new MainViewModel();    
  5.           
  6.     public HomeWindow()    
  7.     {    
  8.         InitializeComponent();    
  9.         _mainViewModel = new MainViewModel();    
  10.              
  11.         DataContext = _mainViewModel;    
  12.     }    
  13.     private void LaalKitab(object sender, RoutedEventArgs e)    
  14.     {    
  15.         MainWindow mainWindow = new MainWindow(_mainViewModel);    
  16.         _mainViewModel = new MainViewModel();    
  17.               
  18.         DataContext = _mainViewModel;    
  19.         mainWindow.Show();    
  20.     }  
  21. }   
Function LaalKitab opens a window but how to set the value of comboBox in the LaalKitab function itself and pass it so it changes the user control in Main Window content area?
 
It works perfectly when I change the value from comboDropdown at runtime.
 
Thanks in advance 

Answers (1)