I have been tasked with adding a combobox to an already existing WPF form. I have never worked with WPF and I am pretty lost, specifically when it comes to binding and using the ObservableCollection property. All the examples are completely different form the way I was told to do it.
I originally had my combobox set up like this:
<ComboBox Name="GroupComboBox" Width="132" Height="22" Grid.Column="0" Grid.Row="3" Margin="0,30" VerticalAlignment="Top" >
<ComboBoxItem Content="Data Warehouse"></ComboBoxItem>
<ComboBoxItem Content="KPI"></ComboBoxItem>
<ComboBoxItem Content="Failures"></ComboBoxItem>
<ComboBoxItem Content="All Groups"></ComboBoxItem>
</ComboBox>
Which worked great. This was when I was told I had to get rid of all the ComboBoxItem Contents and bind the combo box to ObservableCollectionGroups and ObservableCollectionSelectedGroups and to do so all I had to do was add this to the ViewModel class:
public ObservableCollection<string> Groups { get; set; }
public ObservableCollection<string> SelectedGroups { get; set; }
OK, so I added the above to the view model class like this:
public class ClioViewModel : INotifyPropertyChanged
{
public ObservableCollection<string> Groups { get; set; }
public ObservableCollection<string> SelectedGroups { get; set; }
}
(there’s a ton of other stuff already in this class as well but in the interest of time and space I did not post it. If needed I will gladly add more if requested)
I then changed my xaml to look like this:
<ComboBox Name="GroupComboBox" ItemsSource="{Binding Groups}" SelectedItem=" Binding SelectedGroups, Mode=TwoWay}" Width="132" Height="22" Grid.Column="0" Grid.Row="3" Margin="0,30" VerticalAlignment="Top" >
</ComboBox>
which didn’t work. Of course it didn’t work! I haven’t listed any of the items I want inside of my comboBox! The question is, if they do not belong inside of the comboBox and they are not put in the Group/Selected group properties, where in the heck do they go? None of the multitude of combobox binding examples I have seen look anything like what I am being told to do.
If someone could enlighten me on what I am missing, I would really appreciate it.
you need to add the values in your
Groupcollection somewhere (in the initializer or the class constructor I’d say):frankly, I do not see the point of doing this in this case, but It might have something to do with the rest of your code.