使WPF中的列表框项不可选

我有一个WPF的列表框,当他们select一个项目,它显示一个难看的颜色我可以使所有的项目不可选?

如果您不需要select,请使用ItemsControl而不是ListBox

在ListBoxItem样式中将Focusable属性添加为false:

 <Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}"> <!-- Possibly other setters --> <Setter Property="Focusable" Value="False" /> </Style> 

如果你不想让他们select,那么你可能不想要一个列表视图。 但如果这是你真正需要的,那么你可以用一种风格来做到这一点:

 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Page.Resources> <Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border Name="Border" Padding="2" SnapsToDevicePixels="true"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="true"> <Setter TargetName="Border" Property="Background" Value="#DDDDDD"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="#888888"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Page.Resources> <Grid> <ListBox> <ListBoxItem>One</ListBoxItem> <ListBoxItem>Two</ListBoxItem> <ListBoxItem>Three</ListBoxItem> </ListBox> </Grid> </Page> 

看看IsSelected触发器。 您可以使边框具有不同的颜色,使其不是“丑陋”或将其设置为透明,并且在选定时不会显示。

希望这可以帮助。

请在你的列表框中使用这个。 我发现这非常优雅的解决scheme

 <ListBox ItemsSource="{Binding YourCollection}"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Focusable" Value="False"/> </Style> </ListBox.ItemContainerStyle> </ListBox> 

还有一个更简单的方法:设置ListBox属性IsHitTestVisible="False" 。 这可以防止列表中的所有项目接收鼠标事件。 这样做的好处是可以在鼠标hover时停止高亮显示。

它适用于WP 7.1。

一个简单的方法(使用上面的viky的答案)是将SelectionChanged()中的选定索引设置为-1,如下所示。

 public void OnListView_SelectionChanged(Object sender, RoutedEventArgs e) { if (null != sender && sender is ListView) { ListView lv = sender as ListView; lv.SelectedIndex = -1; } } 

更好地避免事件,它更优雅,没有副作用的风格标签。

 <ListBox> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="IsEnabled" Value="False"/> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> ... what you want as a source ... </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

您可以处理ListBox的SelectionChanged事件,并取消select事件处理程序中的选定项目。

如果有人仍然希望不可选的ListBoxItem(或ListViewItem)function。 http://thrash505.wordpress.com/2011/01/04/non-selectable-listboxitem-or-listviewitem-using-attached-properties/

你也可以制作禁用的Listbox,它会给你静态的,非交互式的列表框。

 <ListBox IsEnabled="False"/> 

我认为这是尽可能简单的解决scheme。

在我的情况下,我有一个Textbox和一个combobox模板ListboxItems。 唯一的“积极”应该是组合…

 <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" CanContentScroll="True" /> <ItemsControl> ....here my content.... </Itemscontrol> </ScrollViewer> 

按照预期为我工作。 BR,Daniel