I cann't find the error. The first two usercontrol view with combobox is ok, but not the last two.

simsenVejle

Well-known member
Joined
Feb 14, 2021
Messages
46
Programming Experience
Beginner
Hi,
I'm doing my project and I have now in several hours looked at it but cann't finde the error.

I have 2 views 1 view is normal mode, where there is a combobox where it should be updated when choosing the tabItem is chosen. 2. View is new (creating a new item). Here is also a combobox with items, that should be updated.

Code for the first that works = update combobox in both views:

1 View (that works):
First ComboBox in normal view:
<ComboBox x:Name="CbxEnvironment" Grid.Row="4" Grid.Column="2" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center"
                                          ItemsSource="{Binding Path=Environment_GetActive}" DisplayMemberPath="EnvironmentName"
                                          SelectedValuePath="EnvironmentId"
                                          Text="{Binding ElementName=LivEnvironmentVersions, Path=SelectedValue.EnvironmentName, Mode=TwoWay}"
                                          SelectedValue="{Binding ElementName=LivEnvironmentVersions, Path=SelectedValue.EnvironmentId, Mode=TwoWay}"
                                           Style="{StaticResource ComboBox}">
                                </ComboBox>

2. View (that works)
2. Views combobox that works:
<ComboBox x:Name="CbxEnvironmentNameActiveNew" Grid.Row="5" Grid.Column="2" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center"
                                          ItemsSource="{Binding Path=Environment_GetActive}" DisplayMemberPath="EnvironmentName"
                                          SelectedValuePath="EnvironmentId"
                                          SelectedValue="{Binding EnvironmentId}"
                                           Style="{StaticResource ComboBox}">
                </ComboBox>

Both of the above views has the following code in the cs file

DataqContext:
InitializeComponent();
                DataContext = ViewModel.Account.EnvironmentVersionViewModel.GetInstance();

The same ViewModel is used on the 2 above views

ViewModel:
public EnvironmentVersionViewModel()
            {
                LoadEnvironmentVersions();
                LoadEnvironments();
                CmdSave = new MyICommand(SaveChanges);
                CmdSaveNew = new MyICommand(SaveNew);
                CmdSaveInactive = new MyICommand(SaveInactiveChanges);
                CmdUpdate = new MyICommand(Update);
            }
    
    //This one I use to update the Environments
    public MyICommand CmdUpdate { get; set; }
            public void Update()
            {
                ResetMessages();
                LoadEnvironments();
            }
    
    public void LoadEnvironments()
            {
                DalEnvironment dalEnvironments = new DalEnvironment();
                var environments = dalEnvironments.GetEnvironments();
    
                if (environments != null)
                {
                    List<Environment> Environment_GetAllList = environments;
                    if (Environment_GetAllList != null)
                    {
                        Environment_GetAll = new ObservableCollection<Environment>(Environment_GetAllList);
                    }
                    else
                    {
                        Environment_GetAll = null;
                    }
    
                    //Active
                    List<Environment> Environment_GetActiveList = environments.Where(x => x.EnvironmentIsObsolete == false).ToList();
                    if (Environment_GetActiveList != null)
                    {
                        Environment_GetActive = new ObservableCollection<Environment>(Environment_GetActiveList);
                    }
                    else
                    {
                        Environment_GetActive = null;
                    }
    
                    //Inactive
                    List<Environment> Environment_GetInactiveList = environments.Where(x => x.EnvironmentIsObsolete == true).ToList();
                    if (Environment_GetInactiveList != null)
                    {
                        Environment_GetInactive = new ObservableCollection<Environment>(Environment_GetInactiveList);
                    }
                    else
                    {
                        Environment_GetInactive = null;
                    }
                }
            }

All above works as it should do = update both the comboBoxes in the normal and new view

Now I want to do the same with 2 other rows (and another ViewModel), but here It goes wrong. It doesn't update the normal view (the comboBox here) but updates the new view (the combobox for new view)
The Normal View combobox::
<ComboBox x:Name="CbxProject" Grid.Row="5" Grid.Column="2" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center"
                                          ItemsSource="{Binding Path=Projects_GetActive}" DisplayMemberPath="ProjectName"
                                          SelectedValuePath="ProjectId"
                                          SelectedValue="{Binding ProjectId}">
                                </ComboBox>

The New View combobox::
<ComboBox x:Name="CbxProjectCategoryActiveNew" Grid.Row="6" Grid.Column="2" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center"
                                          ItemsSource="{Binding Path=Projects_GetActive}" DisplayMemberPath="ProjectName"
                                          SelectedValuePath="ProjectId"
                                          SelectedValue="{Binding ProjectId}">
                </ComboBox>

Both link datacontext to the same ViewModel::
InitializeComponent();
                DataContext = ViewModel.Account.CategoryViewModel.GetInstance();

CategoryViewModel:
public CategoryViewModel()
            {
                LoadCategories();
                LoadProjects();
                CmdSave = new MyICommand(SaveChanges);
                CmdSaveNew = new MyICommand(SaveNew);
                CmdSaveInactive = new MyICommand(SaveInactiveChanges);
                CmdUpdate = new MyICommand(Update);
            } 

    public MyICommand CmdUpdate { get; set; }
            public void Update()
            {
                ResetMessages();
                LoadProjects();
            }
    
    public void LoadProjects()
            {
                DalProjects dalProjects = new DalProjects();
                var projects = dalProjects.GetProjects();
    
                if (projects != null)
                {
                    //Active
                    Projects_GetActive = projects.Where(x => x.ProjectIsActive == true).ToList();
                }
            }

Can you please help me to see, where I'm doing something wrong?

If You want to see other code, please let me know. Nothing is secret in my project :)

Best regards
Simsen :)
 
Back
Top Bottom