Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
Bind SelectedItem of Listbox group content


how can I bind the SelectedItem of a Listbox group content? In the sample project "xamOutlookBar - Data - Adding Groups with Data Binding", a listbox is filled, but without an interaction, e.g. when the user clicks on the list item.

I tried to set the SelectedItem property in the DataTemlate section, but could not bind to my public SelectedItem property. I need to know which item was clicked, and also want to highlight an item.

 <DataTemplate x:Key="outlookbarGroupItemTemplate">
            <ListBox ItemsSource="{Binding}" SelectedItem={Binding ???}>



  • 28670
    Verified Answer
    Offline posted

    Hello Bin Cui,

    In the samples browser project that you have referenced, the data context of the ListBox in the DataTemplate will be the collection of "sub-tasks" or items for a particular group. Unfortunately this disallows you to bind directly to the underlying group item, unless you were to perhaps create a custom collection that had a reference to it.

    Rather than doing that, I would recommend that you create a property on your underlying group item that returns the item that should be selected, and then you can bind up to the XamOutlookBar via a RelativeSource AncestorType binding. The XamOutlookBar has a SelectedGroup property, and the data context of this group will be the underlying data item to that group. Knowing this, you can craft a SelectedItem binding like so:

    SelectedItem="{Binding RelativeSource={RelativeSource AncestorType={x:Type ig:XamOutlookBar}}, Path=SelectedGroup.DataContext.SelectedSubTaskProperty}"

    I have attached a sample project to demonstrate the above.

    Please let me know if you have any other questions or concerns on this matter.

    Associate Developer