Resolved change the selected Value of a bound combo box from a window button?

destro

Active member
Joined
Mar 28, 2020
Messages
36
Programming Experience
1-3
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:
RibbonViewModel.cs:
namespace ManglamStellaris.ViewModels
{
    public class RibbonViewModel : ViewModel
    {
        public event Action<PackageType> OnPackageSelection;

        public RibbonViewModel()
        {

            ListPackageType = new List<Tuple<PackageType, string>>(new Tuple<PackageType, string>[]
            {
                   new Tuple<PackageType, string>(PackageType.Raw, "Raw"),
                   new Tuple<PackageType, string>(PackageType.Lagna, "Lagna"),
                   new Tuple<PackageType, string>(PackageType.LaalKitab, "LaalKitab")

            });

        }
       
        public List<Tuple<PackageType, String>> ListPackageType { get; private set; }

        public PackageType packageType
        {
            get { return _packageType; }
            set
            {
                _packageType = value;
                RaisePropertyChanged("packageType");

                if (OnPackageSelection != null)
                {
                    OnPackageSelection(_packageType);
                }
            }
        }

        private PackageType _packageType;

    }
}

An enum PackageType is defined in enums.cs
[enums].cs:
public enum PackageType
    {
        Raw,
        Lagna,
        LaalKitab
    }

RibbonViewModel is initialized in MainViewModel as follows:
MainViewModel.cs:
  public class MainViewModel : ViewModel
    {
        Dictionary<Type, object> _Services = new Dictionary<Type, object>();
        public event Action<PackageType> OnPackageSelection;
        public MainViewModel() {
            Config = new ConfigViewModel();
            Input = new InputViewModel();
            Result = new CalculationResultViewModel();
            RibbonViewModel = new RibbonViewModel();
            RibbonViewModel.OnPackageSelection += (PackageType) => {
                if (OnPackageSelection != null) {
                    OnPackageSelection(PackageType);
                }
            };

            DoCalculationCommand = new RelayCommand(() => {
                DoCalculation();
            });
        }

MainWindow controls the user control shown in the content control area of Main Window based on selection type:
MainWindow.xaml.cs:
public partial class MainWindow : Window
    {
        public MainViewModel ViewModel;

        public ResultView ResultView;
        public LagnaChart LagnaChart;
        public LaalKitab LaalKitab;

        public MainWindow(MainViewModel mainViewModel)
        {
            InitializeComponent();
            ViewModel = mainViewModel;
            ViewModel.OnPackageSelection += (PackageType) => {
                if (PackageType == PackageType.LaalKitab)
                {
                    this.mainContentArea.Content = LaalKitab;
                }
                else if (PackageType == PackageType.Lagna)
                {
                    this.mainContentArea.Content = LagnaChart;
                }
                else if (PackageType == PackageType.Raw)
                {
                    this.mainContentArea.Content = ResultView;
                }
            };

            DataContext = ViewModel;

            ResultView = new ResultView(ViewModel);
            LagnaChart = new LagnaChart(ViewModel);
            LaalKitab = new LaalKitab(ViewModel);

            this.mainContentArea.Content = ResultView;
        }
    }

How can I select the combo box value from another Window Button Click function which is as follows:

C#:
  public partial class HomeWindow : Window
    {

        public LagnaChart LagnaChart;
        MainViewModel _mainViewModel = new MainViewModel();
      
        public HomeWindow()
        {
            InitializeComponent();
            _mainViewModel = new MainViewModel();
         
            DataContext = _mainViewModel;
        }
private void LaalKitab(object sender, RoutedEventArgs e)
        {

            MainWindow mainWindow = new MainWindow(_mainViewModel);
            _mainViewModel = new MainViewModel();
          
            DataContext = _mainViewModel;
            mainWindow.Show();
        }

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
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,392
Location
Chesapeake, VA
Programming Experience
10+
I haven't looked closely at the code, but the idea in WPF is that the model is the source of truth. As long as you update the model, and all the view models that present data from that model are notified of the change, then the view model, by its nature as a property notification source, will end up updating all the views. If you are using the view as your model you are doing something wrong., If your view model is not storing data back into the model, or not updating itself when the model is updated, then you are doing something wrong.
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,877
Location
UK
Programming Experience
10+
public MainViewModel ViewModel; should be declared as new for the cycle duration of your window.
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,877
Location
UK
Programming Experience
10+
I see you're declaring it as new after initialising on line 10. Don't do it like that, just declare it as new on line 2.
 

destro

Active member
Joined
Mar 28, 2020
Messages
36
Programming Experience
1-3
This is how I got it to work. RibbonViewModel is initialized in constructor of mainViewModel so I accessed it and changed the package type as follows:
Do you think its the right way?
HomeWindow.xaml.cs:
  private void LaalKitab(object sender, RoutedEventArgs e)
        {

       
            MainWindow mainWindow = new MainWindow(_mainViewModel);

            _mainViewModel.RibbonViewModel.packageType = PackageType.Lagna;

            _mainViewModel = new MainViewModel();
         
            DataContext = _mainViewModel;
         
            mainWindow.Show();
           
        }
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,877
Location
UK
Programming Experience
10+
When you declare objects a new in a method, they are declared a new only for the duration of that methods life cycle, unless you either return the object, or pass it to another method.

My previous advice still stands.
 
Top Bottom