2

I have 2 comboboxes each with 2 methods for _Loaded and _Selection changed

i want to select a location in the first combobox and then the next combobox should list a bunch of dates for that specific location

Here is what i have so far:

    <ComboBox 
    x:Name="comboBoxLocation" 
    Text="Lokation"       
    HorizontalAlignment="Left" Margin="50,305,0,0" 
    VerticalAlignment="Top"  
    Width="120" 
    Loaded="ComboBoxLocation_Loaded" 
    SelectionChanged="ComboBoxLocation_SelectionChanged"/>

    <ComboBox x:Name="comboBoxDate" 
    Text="Dato" HorizontalAlignment="Left" 
    Margin="195,305,0,0" 
    VerticalAlignment="Top" Width="120" 
    Loaded="ComboBoxDate_Loaded"  
    SelectionChanged="ComboBoxDate_SelectionChanged"/>

and

    private void ComboBoxLocation_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var comboBoxLocation = sender as ComboBox;

        comboBoxDate.SelectedIndex = 0;
        comboBoxDate.ItemsSource = controller.GetBusTimes();

        //ComboBoxDate_Loaded(sender, e);
    }

    private void ComboBoxDate_Loaded(object sender, RoutedEventArgs e)
    {
        List<string> dataDate = controller.GetBusTimes();

        var comboBoxDate = sender as ComboBox;

        comboBoxDate.ItemsSource = dataDate;
    }

This seems to be a lot more difficult than i expected... I am starting to think that i might have madesome fundamental mistake here...

I have been fiddling around with this... I can manage to show a list of locations in the first box and the relevant dates for that location the second box. But when i change the first location, the dates stay the same...

How would i go about this?

Dunark
  • 23
  • 3
  • I would bind the selected item of the first combobox to a property in your viewmodel and in the setter I would change the collection (which should be observable) of items in another property, which is also bound in the GUI. Do you know what i mean? – Sebastian Siemens May 20 '16 at 12:09
  • *"have madesome fundamental mistake"* - you are using wpf like it's winforms, so you inherit all binding issues of latter. With the MVVM pattern use it's very easy to create master/details relationship of data. E.g. binding collection of items to one combobox `ItemsSource` (and displaying only location), which `SelectedItem` is bound to property in setter of which you update details (create another collection and rise notification) bound to second combobox `ItemsSource`. – Sinatr May 20 '16 at 12:10
  • And your mistake is actually trying to use `Loaded` event. It's rised only once. Rather re-set `comboBoxDate.ItemsSource` in the `comboBoxLocation_SelectionChanged`. I see you tried that (commented line), you may have to do it in 2 steps (setting it first to `null`). – Sinatr May 20 '16 at 12:12
  • 1
    I'm not sure he has heard or used MVVM. Maybe someone should introduce how MVVM makes it easy to accomplish what he wants. – afaolek May 20 '16 at 12:26

3 Answers3

0

Try this, I have used this to link 2 comboboxes in my windows form application.

By default set both comboboxes selection to 0 //Combobox1.SelectedIndex = 0;

 private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       var combobox1VALUE= combobox1.Text;
    }

private void combobox2_DropDown(object sender, EventArgs e)
        {
           //Select datasouece according to combobox 1 data selection(combobox1VALUE) 
            combobox2.Items.Clear();
          //add data to combobox2

        }
Jaimesh
  • 841
  • 4
  • 25
  • 41
0

There are 2 issues with your code.

  1. It is not clear where & how controller.GetBusTimes() gets information about changed location? If somehow the above-mentioned function is in know of location change and is still not showing updated info in another combo box then

  2. See also this answer on how to refresh combo box display once ItemSource is changed

Community
  • 1
  • 1
Mukesh Adhvaryu
  • 642
  • 5
  • 16
0

Why not to bind separately Item source and selected item?

<Combobox ItemsSource="{Binding ItempsProperty}" SelectedItem="{Binding StrValueProperty, Mode=TwoWay}" />

And you could set one property in the setter of other

curiousity
  • 4,703
  • 8
  • 39
  • 59