2012-12-17 74 views
1

List我存储的几个项目有头和子项列表框 - 选择行为

_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); 

在WPF我结合这些项目的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> 

我现在尝试,但失败是使仅在内部ListBox点击的项目,即要避免:

enter image description here

如果我设置TextBlock.IsEnabledTextBlock.IsHitTestVisible假没有什么变化。如果StackPanel's属性设置为false,则内部ListBox不再可点击,但有趣的是TextBlock仍然是可以点击的。而外部的ListBox's属性阻止点击任何东西。

如果外层ListBox改为ListView,则行为相同。

我还没有想出什么,我需要改变,以确保只有在内部列表中的项目启用。有任何想法吗?

+0

的评论@HighCore感谢。最后,这与使用IsEnabled具有相同的效果。 – Em1

+0

您是否尝试过使用'ItemsControl'作为外部列表? – Niki

回答

2

如果没有必要在外部ListBox任何选择的项目,不要使用ListBox - 使用ItemsControl代替:

<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 > 
+0

那么,起初我认为你是对的,但它根本不会改变任何东西。 – Em1

+2

你可能是指'ItemsControl'。 'ListView'是'Selector'的后代,所以它是用来选择项目的。 – Niki

+0

@nikie你当然是对的。我搞砸了。感谢您指出了这一点!给自己留言:想想 - 再想一想 - 写。 – Spontifixus