none
How can I make a Master/Slave with 2 ComboBox?

    Question

  • Hi,

    I have 2 ComboBox one is loaded with the day of the week

      private string[] SHORT_DAY_NAMES = new string[] { "Mon", "Tue"};

    and the second must be loaded with one of to classes of day

      private string[] Mon = new string[] { "Math", "Gym", "Lunch" };

      private string[] Tue = new string[] { "English", "Spanish", "Lunch" };

    What I need is when I select in first Mon in second to have binding to Mon array.

    Thanks


    ADRIAN DIBU

    Tuesday, July 23, 2013 6:46 PM

Answers

  • Are you setting the selected index in XAML (SelectedIndex="0")? if yes remove it, if you want to have the first item selected by defaul, do it after your items are loaded since you can't select index 0 when there are no items in your ComboBox.

    I am not sure why you are setting the selected index to zero in the selection changed event handler (The user will select item and you will change it back to the first item, your selected item will always be the first item), if this is the desired result and you are trying to avoid the -1 index, just check the selected index and ignore it when it is -1.

    private void ComboBoxClasses_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {

        if (comboBoxClasses.SelectedIndex != -1)

        {

            comboBoxClasses.SelectedIndex = 0; // <---this looks wrong to me :(

            //do your cool stuff here

        }

    }

    Good luck,

    Hassan

    Wednesday, July 24, 2013 4:59 AM

All replies

  • On selection change of first combo box you can update the item source for second combo box. 

    Thanks,
    Sachin
    My Samples

    Tuesday, July 23, 2013 7:38 PM
  • Why not create list of days ("Mon", "Tue") that has list of work items ("Math", "Gym"....), bind the main combo box to the days list and then bind the second combo box to the selected item of the main combo box. Hopefully the below code will give you some ideas.

    Create a class for item I am calling it work item

    public class WorkItem

    {

        public string Work { get; set; }

    }

    create class for workload of one day I am calling it DayWorkload

    public class  DayWorkLoad

    {

        public List<WorkItem> Items { get; set; }

        public string DayOfTheWeek { get; set; }

    }

    I am not sure how you are handling your data but you can populate the data in the code behind or in your view model. Code behind:  In the load state of your page create a list of workload that will hold all your weekday work items I am calling it workload, lists of day items, I am calling them mondayItems, tuesdayItmes ..... fill all lists with your data and set the page DataContext to the main list.

    private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)

    {

        List<DayWorkLoad> workload = new List<DayWorkLoad>();

        List<WorkItem> mondayItems = new List<WorkItem>();

        List<WorkItem> tuesdayItems = new List<WorkItem>();

        mondayItems.Add(new WorkItem() { Work = "Math" });

        mondayItems.Add(new WorkItem() { Work = "Gym" });

        mondayItems.Add(new WorkItem() { Work = "Lunch" });

        tuesdayItems.Add(new WorkItem() { Work = "English" });

        tuesdayItems.Add(new WorkItem() { Work = "Spanish" });

        tuesdayItems.Add(new WorkItem() { Work = "Lunch" });

        workload.Add(new DayWorkLoad() { DayOfTheWeek = "Mon", Items = mondayItems });

        workload.Add(new DayWorkLoad() { DayOfTheWeek = "Tue", Items = tuesdayItems });

        this.DataContext = workload;

    }

    Now you can set the ItemsSource of the main ComboBox to Binding (you will also have to create DataTemplate)

    <ComboBox x:Name="daysCombo"

              HorizontalAlignment="Left"

              VerticalAlignment="Top" Width="120"

              ItemsSource="{Binding}" Grid.Row="1"

              ItemTemplate="{StaticResource daysComboDataTemplate}"/>

    DataTemplate will look like below

    <DataTemplate x:Key="daysComboDataTemplate">

        <Grid>

            <TextBlock HorizontalAlignment="Left"

                       TextWrapping="Wrap"

                       VerticalAlignment="Top"

                       Text="{Binding DayOfTheWeek}"/>

        </Grid>

    </DataTemplate>

    Bind the ItemsSource of the second ComboBox to the selected items of the main ComboBox (you will have to create DataTemplate).

    <ComboBox x:Name="workCombo" HorizontalAlignment="Left"

              VerticalAlignment="Top" Width="120"

              Grid.Row="1" Margin="500,0,0,0"

              ItemsSource="{Binding SelectionBoxItem.Items, ElementName=daysCombo}"

              ItemTemplate="{StaticResource workComboDataTemplate}"/>

    The DataTemplate for the second ComboBox will look like below

    <DataTemplate x:Key="workComboDataTemplate">

        <Grid>

            <TextBlock HorizontalAlignment="Left"

                       TextWrapping="Wrap"

                       Text="{Binding Work}" VerticalAlignment="Top"/>

        </Grid>

    </DataTemplate>

    Hope this helps,

    Hassan


    Tuesday, July 23, 2013 10:54 PM
  • Hi,
    Thanks for answers. I learned a lot.

    My problem  is if I don't use
    comboBoxClasses.SelectedIndex = 0;
    I have null error because comboBoxClasses.SelectedIndex = -1

    If I use it I can't select items because it reads the first item.

    I can't understand how can I manage SelectedIndex and SelectionChanged.
    Is it a way to know the value of SelectedIndex when SelectionChanged.

     private void ComboBoxClasses_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
               comboBoxClasses.SelectedIndex = 0;
                selectedClassI = comboBoxClasses.SelectedIndex;
            }

      private void ComboBoxDaytName_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                strDayOfWeekNameJ = comboBoxDayName.SelectedItem.ToString().Trim();

                DayOfWeekJI = 1 + Array.IndexOf(SHORT_DAY_NAMES, SHORT_DAY_NAMES.Where(x => x.Contains                (strDayOfWeekNameJ)).FirstOrDefault());
                strDayOfWeekJ = DayOfWeekJI.ToString();

                if (viewIndex.Equals("1")) // Short week
                {
                    dw1 = (comboBoxDayName.SelectedIndex + 2).ToString();
                }
                else
                {
                    dw1 = (comboBoxDayName.SelectedIndex + 1).ToString();

                }
                loadClasses(dw1);
            }


    ADRIAN DIBU

    Wednesday, July 24, 2013 1:33 AM
  • Are you setting the selected index in XAML (SelectedIndex="0")? if yes remove it, if you want to have the first item selected by defaul, do it after your items are loaded since you can't select index 0 when there are no items in your ComboBox.

    I am not sure why you are setting the selected index to zero in the selection changed event handler (The user will select item and you will change it back to the first item, your selected item will always be the first item), if this is the desired result and you are trying to avoid the -1 index, just check the selected index and ignore it when it is -1.

    private void ComboBoxClasses_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {

        if (comboBoxClasses.SelectedIndex != -1)

        {

            comboBoxClasses.SelectedIndex = 0; // <---this looks wrong to me :(

            //do your cool stuff here

        }

    }

    Good luck,

    Hassan

    Wednesday, July 24, 2013 4:59 AM
  • Hi,

    I made comboBoxClasses.SelectedIndex = 0 because I wasn't able to determine what is the index for SelectionChanged.

    I don't have in XAML SelectedIndex="0".

    I need the index for details.

    Thanks

    ADRIAN DIBU

    Wednesday, July 24, 2013 12:15 PM
  • Hi,

    In fact what I want is to get the value of  Index when SelectionChanged

    Thanks

     

     


    ADRIAN DIBU

    Wednesday, July 24, 2013 4:02 PM
  • Hi,

    I solved the problem.

    SelectionChanged event was commented.

    Thanks for help


    ADRIAN DIBU

    Wednesday, July 24, 2013 5:21 PM