Setting SelectedValue doesn't update combobox

simsenVejle

Active member
Joined
Feb 14, 2021
Messages
31
Programming Experience
Beginner
Hi,
I have problems getting the combobox to work.

I have a usercontrol with an listview and some controls. Everything works fine, when not using the combobox. Each time I click on one row in the ListView it should show in the controls (textboxes, labels and so on).

The list in the listview is of Category and here I have a key of ProjectId and a key of ProjectName. In the CategoryViewModel I have made an property SelectedCategory. No problems there at all

It works fine except I cann't update the combobox.

The combobox I take an ItemsSource of a list of Projects. Now I want to bind the ListView (LivCategories) with the ComboBox, so when selecting af Category, it shows me the project in the combobox for that specific Category. And I also want it to, if there isn't any projectId for a specific Category it should leave the combobox blank (or something to show here is no project)

I get the items by binding Projects_GetActive to the combobox (whitch succeded).

You can see in my code, I have made 2 textboxes, with ProjectId and ProjectName. Those two updates every time I select a new row in the Listview. So I have the right SelectedCategory. I do not know how to update the combobox.

If You miss some code please let me know - I don't have any secret part at all.

Best regards
Simsen :)


My two Textboxes and the combobox that won't work:
<ComboBox x:Name="CbxProject" Text="Projekt" Grid.Row="5" Grid.Column="2" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center"
                                      ItemsSource="{Binding Projects_GetActive}" DisplayMemberPath="ProjectName"
                                      SelectedItem="{Binding ElementName=LivCategories, Path= SelectedValue.ProjectName, Mode=TwoWay,
                                UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True">
                            </ComboBox>

                            <TextBox x:Name="TxtProjectId" Margin="0 0 8 0" Text="{Binding ElementName=LivCategories, Path = SelectedValue.ProjectId, Mode = TwoWay}" Grid.Row="6" Grid.Column="1" Height="25" />
                            <TextBox x:Name="TxtProjectName" Margin="0 0 8 0" Text="{Binding ElementName=LivCategories, Path = SelectedValue.ProjectName, Mode = TwoWay}" Grid.Row="6" Grid.Column="2" Height="25" />

My ListView:
<ListView x:Name="LivCategories" ItemsSource="{Binding Categories_GetActive}" SelectedItem="{Binding SelectedCategory}" Grid.Row="0" Grid.Column="0">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Id" Width="50" DisplayMemberBinding="{Binding CategoryId}"/>
                                <GridViewColumn Header="Navn" Width="250"  DisplayMemberBinding="{Binding CategoryName}"/>
                                <GridViewColumn Header="Global" Width="50"  DisplayMemberBinding="{Binding CategoryIsGlobal}"/>
                                <GridViewColumn Header="Project" Width="150"  DisplayMemberBinding="{Binding ProjectName}"/>
                                <GridViewColumn Header="ProjectId" Width="0"  DisplayMemberBinding="{Binding ProjectId}" />
                            </GridView>
                        </ListView.View>
                    </ListView>

SelectedCategory in CategoryViewModel:
private Category _selectedCategory;
        public MyICommand DeleteCommand { get; set; }

        public Category SelectedCategory
        {
            get
            {
                return _selectedCategory;
            }

            set
            {
                _selectedCategory = value;
                OnPropertyChanged("SelectedCategory");
                DeleteCommand.RaiseCanExecuteChanged();
            }
        }

INotifyPropertyChanged Members in CategoryViewModel:
#region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
 
Solution
Ah, so it looks like you are not updating the selected item in your combo box anymore by binding to the list view's selected item. You are just updating the text in the combobox by binding to the list view's selected item.
You are most likely right (I'm not so in it at this moment to see it) - I have changed it a little bit - and it works the way I want it to, so the combox also updates the ListView when changing in the combobox and both places changes the SelectedCategory


C#:
<ComboBox x:Name="CbxProject" Grid.Row="5" Grid.Column="2" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center"
                                      ItemsSource="{Binding...

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,892
Location
Chesapeake, VA
Programming Experience
10+
Why can't you make both the ListView and the ComboBox both bind to the SelectedCategory? Why is the combo box bound to the list view's SelectedItem.ProjectName property. I can how that will work in one direction going from the list view to the combobox, but going the other direction from the combobox to the ListView, how will setting that property cause the list view to change it's currently selected item?
 

simsenVejle

Active member
Joined
Feb 14, 2021
Messages
31
Programming Experience
Beginner
Why can't you make both the ListView and the ComboBox both bind to the SelectedCategory? Why is the combo box bound to the list view's SelectedItem.ProjectName property. I can how that will work in one direction going from the list view to the combobox, but going the other direction from the combobox to the ListView, how will setting that property cause the list view to change it's currently selected item?
I haven't programmed for many years just started trying again. So the many whys is I don't know - just seen it to been done that in several youtube videos.

What I want (don't care how) is that the Listview shows all active Categories. Then I have some controls, so when the user select a row on the ListView, then this should be set in the controls (Labels, Textboxes and a single combo box.

In the combobox I on start up of the usercontrol list of active Projects. Then when selecting a row in the listbox, that specific Categorys projectname if there is an project added to the the specific Category.

I have first recently learned about mvvm and in one of the videos he went for using a SelectedItem (in my case SelectedCategory). That so I can bind the controls with the Listview. That worked well for my. The CategoryId, name and so on where listet in the SelectedCategory. Except from the Combobox - that don't want to do anything except for listing the active projects.

So to your first question - I thought I bound both the ListView and the controls (including the combobox) to the SelectedCategory - this I don't and why?

For your other question. It doesn't have to bound both ways -to me it is important that when selecting a row in the listview, that I can show the single properties in both textboxes (which works fine right now) and the combobox

Please let me know if You have further questions

Best regards
Simsen :)
 

simsenVejle

Active member
Joined
Feb 14, 2021
Messages
31
Programming Experience
Beginner
I forgot 2 things;

If it just this you can't figure out, I have tried 20-25 different suggestion (when googling the problem) but with no success. That's why I am asking in here because I just doesn't know what to do.


Combobox:
<ComboBox x:Name="CbxProject" Text="Projekt" Grid.Row="5" Grid.Column="2" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center"
                                      ItemsSource="{Binding Projects_GetActive}" DisplayMemberPath="ProjectName"
                                      SelectedItem="{Binding ElementName=LivCategories, Path= SelectedValue.ProjectName, Mode=TwoWay,
                                UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True">
                            </ComboBox>

I don't know how to save Selected (and changed)Category if I do not use an Selected. How can I from the viewmodel get the changes from the usercontrol, if I do not put them in e.g. an property?
 

simsenVejle

Active member
Joined
Feb 14, 2021
Messages
31
Programming Experience
Beginner
A very nice man told me what to do:


C#:
<ComboBox x:Name="CbxProject" Grid.Row="5" Grid.Column="2" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center"
                                      ItemsSource="{Binding Path=Projects_GetActive}" DisplayMemberPath="ProjectName"
                                      Text="{Binding ElementName=LivCategories, Path=SelectedValue.ProjectName}">
                            </ComboBox>
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,892
Location
Chesapeake, VA
Programming Experience
10+
Ah, so it looks like you are not updating the selected item in your combo box anymore by binding to the list view's selected item. You are just updating the text in the combobox by binding to the list view's selected item.
 

simsenVejle

Active member
Joined
Feb 14, 2021
Messages
31
Programming Experience
Beginner
Ah, so it looks like you are not updating the selected item in your combo box anymore by binding to the list view's selected item. You are just updating the text in the combobox by binding to the list view's selected item.
You are most likely right (I'm not so in it at this moment to see it) - I have changed it a little bit - and it works the way I want it to, so the combox also updates the ListView when changing in the combobox and both places changes the SelectedCategory


C#:
<ComboBox x:Name="CbxProject" Grid.Row="5" Grid.Column="2" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center"
                                      ItemsSource="{Binding Path=Projects_GetActive}" DisplayMemberPath="ProjectName"
                                      Text="{Binding ElementName=LivCategories, Path=SelectedValue.ProjectName}"
                                      SelectedValue="{Binding LivCategories}">
                            </ComboBox>
 
Solution

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,892
Location
Chesapeake, VA
Programming Experience
10+
Very nice solution!
 
Top Bottom