Em1
Em1

Reputation: 1095

ListBox within ListBox - Selection behavior

In a List I store several items with a header and sub items

_categories = new List<Category>();

Category cOne = new Category() { Header = "Category one" };
cOne.AddItem("Sub One");
cOne.AddItem("Sub Two");
cOne.AddItem("Sub Three");
_categories.Add(cOne);

In WPF I bind these items to a ListBox.

<ListBox x:Name="listbox">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel>
        <TextBlock Text="{Binding Header}" />
        <ListBox ItemsSource="{Binding Subs}" Padding="10 0 0 0" />
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

What I now try but fail is to make only the items in the inner ListBox click-able, i.e. to avoid:

enter image description here

If I set TextBlock.IsEnabled or TextBlock.IsHitTestVisible to false nothing changes. If the StackPanel's properties are set to false the inner ListBox isn't click-able any more but interestingly the TextBlock still is. And the outer ListBox's properties prevents to click anything at all.

The behavior is identical if the outer ListBox is a ListView instead.

I haven't figured out yet what I need to change to make sure that only the items in the inner list are enabled. Any ideas?

Upvotes: 1

Views: 248

Answers (1)

Spontifixus
Spontifixus

Reputation: 6660

If there is no need to select the items in the outer ListBox whatsoever, do not use a ListBox - use an ItemsControl instead:

<ItemsControl x:Name="itemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Header}" />
                <ListBox ItemsSource="{Binding Subs}" Padding="10 0 0 0" />
            </StackPanel>
        </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl >

Upvotes: 2

Related Questions