ComboBox source and selected value binding RRS feed

  • Question

  • Hi all,


    I hope again that someone can help me out with some XAML queries.


    I am adding a ComboBox into a stackpanel which has it's DataContext set to a custom object containing a few simple properties.

    The ComboBox has an ItemSource of type ObservableCollection<string> and this loads fine but I am having issues how to enable the SelectedItem binding from the stackpanel's DataContext object (also of type string).

    I've seen alot of the SelectedValuePath being mentioned but as I am only dealing with string values, I havent got a property to access for this purpose.


    <ComboBox Name="clientComboBox" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Path=Client, Mode=TwoWay}"/>


    Also - Where should I bind the ItemsSource to this ComboBox - I want to do it in code as I have a singleton which maintains all my collections that I want to be available for the GUI's use:


    clientComboBox.ItemsSource = GUICollectionManagerSingleton.getInstance().ClientsCol;


    Could I do this in XAML as a static resource and assign it as a ItemsSource property within the ComboBox's XAML tag?

    Friday, August 31, 2007 6:17 PM


  • hello,

    I think the problem that you have is that you are using the DataContext to set the SelectedItem....

    try this. set the Tag property of the combo box to the item you want to be selected (instead of using the data context) and then alter the binding to this
    SelectedItem="{Binding RelativeSource={RelativeSource Self}, Path=Tag.Client, Mode=TwoWay}"

    I did not try the code but I think that this is the problem. When you set datacontext + ItemsSource, the ItemsControl (in your case the listbox) will set the DataContext of each ListBoxItem as the item in the list so the original datacontext will be replaced....

    try it out and tell me how it went

    Saturday, September 1, 2007 5:14 PM