如果你想用不同的是它内部支持选择的概念没有ListBox
功能的ItemsControl
,您可以轻松创建一个无外观ListBox
是从ItemsControl
没有什么区别。这两个并排,你不能分辨。唯一的区别是左侧列表支持内部选择。
<Grid>
<Grid.Resources>
<x:Array Type="sys:String" x:Key="sampleData">
<sys:String>Red</sys:String>
<sys:String>Green</sys:String>
<sys:String>Blue</sys:String>
</x:Array>
</Grid.Resources>
<UniformGrid Columns="2">
<ItemsControl ItemsSource="{StaticResource sampleData}"/>
<ListBox ItemsSource="{StaticResource sampleData}">
<ListBox.Style>
<Style TargetType="ListBox">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ItemsPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Style>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</UniformGrid>
</Grid>
所以最好的建议是,如果你想选择,从ListBox
派生和样式远离你不想要的东西吧,或者至少从Selector
派生。
为什么选择扩展`ItemsControl`,而不是`Selector`或`ListBox`,它已经实现了选择行为? – 2011-01-07 00:18:01