在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中的项可点击,即避免:

如果我将TextBlock.IsEnabled或TextBlock.IsHitTestVisible设置为false,则不会发生任何更改。如果StackPanel's属性设置为false,则内部ListBox不再可单击,但有趣的是,TextBlock仍然可单击。并且外部的ListBox's属性根本不允许单击任何东西。
如果外部ListBox是ListView,则行为相同。
我还没有弄清楚我需要修改什么,以确保只启用内部列表中的项。有什么想法吗?
发布于 2012-12-18 00:54:04
如果不需要选择外部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 >https://stackoverflow.com/questions/13918286
复制相似问题