locked
Double ComboBox Binding Problem RRS feed

  • Question

  • Hi,

    I have 2 combobox, 1 bound to a list of profiles and the other to the list of presets of a profile. I am trying to change the presets based on the SelectedItem of the first combo box with binding.

    <ComboBox x:Name="m_cmbProfiles" Width="200" ItemsSource="{Binding Profiles, Source={StaticResource ResourceKey=TheTasks}, Converter={StaticResource ResourceKey=debugConverter}}" SelectedIndex="0">
                                                <ComboBox.ItemTemplate>
                                                    <DataTemplate>
                                                        <TextBlock Text="{Binding Name}" />
                                                    </DataTemplate>
                                                </ComboBox.ItemTemplate>
                                            </ComboBox>
                                            <ComboBox x:Name="m_cmbPresets" Width="200" ItemsSource="{Binding Path=SelectedItem.TargetsAsList, ElementName=m_cmbProfiles, Converter={StaticResource ResourceKey=debugConverter}}">
                                                <ComboBox.ItemTemplate>
                                                    <DataTemplate>
                                                        <TextBlock Text="aaaa" />
                                                    </DataTemplate>
                                                </ComboBox.ItemTemplate>
                                            </ComboBox>

     I am unable to bind the SelectedItem.TargetsAsList properly. The profiles will show fine, but even if I change the profile, my presets never does. I also verified that SelectedItem.TargetsAsList has indeed the right data. Also, the binding expression is good (m_cmbPresets.GetBinding(ComboBox.ItemsSourceProperty) constains the right binding). Any idea on that?

    Thanks you


    Thursday, May 15, 2014 2:51 PM

Answers

  • You cannot bind the ItemsSource property to the TargetsAsList property and expect it to get updated when the SelectedItem property of the first ComboBox changes. And what are the converters doing?

    Please refer to my blog post about how to implement cascading ComboBoxes in WPF using the MVVM pattern as this is the recommended way of doing what you are trying to do: http://blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/

    If you don't care about following the MVVM pattern, you should handle the SelectionChanged event of the m_cmbProfiles ComboBox and set the ItemsSource of the other one in this event handler:

            <ComboBox x:Name="m_cmbProfiles" Width="200"
                      ItemsSource="{Binding Profiles, Source={StaticResource ResourceKey=TheTasks}, Converter={StaticResource ResourceKey=debugConverter}}"
                      SelectionChanged="m_cmbProfiles_SelectionChanged"
                      SelectedIndex="0">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
    
            <ComboBox x:Name="m_cmbPresets" Width="200">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="aaaa" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
    

    private void m_cmbProfiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                dynamic item = m_cmbProfiles.SelectedItem;
                if(item != null)
                    m_cmbPresets.ItemsSource = item.TargetsAsList;
            }

    Note that the TargetsAsList property must be of a type that implements IEnumerable to be able to be set as the ItemsSource of a ComboBox.

    Please share your full code in future posts in order for us to be able to help you better.

    Friday, May 16, 2014 8:08 PM

All replies

  • Hi, i think the problem is from

    <DataTemplate>
         <TextBlock Text="aaaa" />
    </DataTemplate>
    textblock.text should bind to something.

    Thursday, May 15, 2014 3:41 PM
  • You cannot bind the ItemsSource property to the TargetsAsList property and expect it to get updated when the SelectedItem property of the first ComboBox changes. And what are the converters doing?

    Please refer to my blog post about how to implement cascading ComboBoxes in WPF using the MVVM pattern as this is the recommended way of doing what you are trying to do: http://blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/

    If you don't care about following the MVVM pattern, you should handle the SelectionChanged event of the m_cmbProfiles ComboBox and set the ItemsSource of the other one in this event handler:

            <ComboBox x:Name="m_cmbProfiles" Width="200"
                      ItemsSource="{Binding Profiles, Source={StaticResource ResourceKey=TheTasks}, Converter={StaticResource ResourceKey=debugConverter}}"
                      SelectionChanged="m_cmbProfiles_SelectionChanged"
                      SelectedIndex="0">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
    
            <ComboBox x:Name="m_cmbPresets" Width="200">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="aaaa" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
    

    private void m_cmbProfiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                dynamic item = m_cmbProfiles.SelectedItem;
                if(item != null)
                    m_cmbPresets.ItemsSource = item.TargetsAsList;
            }

    Note that the TargetsAsList property must be of a type that implements IEnumerable to be able to be set as the ItemsSource of a ComboBox.

    Please share your full code in future posts in order for us to be able to help you better.

    Friday, May 16, 2014 8:08 PM