使 WPF 中的列表框项目不可选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1722456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
make Listbox items in WPF not selectable
提问by Ghassan Karwchan
I have a listbox in WPF, and when they select an item, it shows an ugly colors Can I make all the items non-selectable?
我在 WPF 中有一个列表框,当他们选择一个项目时,它显示出难看的颜色我可以让所有项目都不可选择吗?
回答by Thomas Levesque
If you don't need selection, use an ItemsControl
rather than a ListBox
如果您不需要选择,请使用一个ItemsControl
而不是一个ListBox
回答by Vadym Kyrylkov
Add Focusable property as false in ListBoxItem style:
在 ListBoxItem 样式中将 Focusable 属性添加为 false:
<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}">
<!-- Possibly other setters -->
<Setter Property="Focusable" Value="False" />
</Style>
回答by Asad Durrani
Please use this inside your listbox. I found this very elegant solution
请在您的列表框中使用它。我找到了这个非常优雅的解决方案
<ListBox ItemsSource="{Binding YourCollection}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
回答by 4imble
If you dont want them selectable then you probably dont want a listview. But if this is what you really need then you can do it with a style:
如果您不希望它们可选,那么您可能不需要列表视图。但如果这是你真正需要的,那么你可以用一种风格来做:
<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>
Look at the IsSelected Trigger. You can make the border a different colour so it is not "Ugly" or set it to transparent and it will not be visible when selected.
查看 IsSelected 触发器。您可以将边框设置为不同的颜色,使其不“丑陋”或将其设置为透明,并且在选择时将不可见。
Hope this helps.
希望这可以帮助。
回答by Denise Draper
There's an even easier way: set ListBox
property IsHitTestVisible="False"
. This prevents all the items in the list from receiving mouse events. This has the advantage of stopping the highlighting as you mouse-over as well.
还有一种更简单的方法:设置ListBox
属性IsHitTestVisible="False"
。这可以防止列表中的所有项目接收鼠标事件。这具有在鼠标悬停时停止突出显示的优点。
It works for me in WP 7.1.
它在 WP 7.1 中对我有用。
回答by tornacious
A simple way to do this (using the answer from viky above) is to set the selected index to -1 in the SelectionChanged(), as follows.
执行此操作的一种简单方法(使用上面来自 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;
}
}
回答by user1630939
Better to avoid events, it's more elegant and without side effects the Style tag.
更好地避免事件,它更优雅并且没有副作用 Style 标签。
<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>
回答by viky
you can handle SelectionChanged event of ListBox and unselect the selected item in the event handler.
您可以处理 ListBox 的 SelectionChanged 事件并取消选择事件处理程序中的选定项。
回答by Tim Valentine
In case someone still wants non-selectable ListBoxItem (or ListViewItem) functionality. http://thrash505.wordpress.com/2011/01/04/non-selectable-listboxitem-or-listviewitem-using-attached-properties/
如果有人仍然需要不可选择的 ListBoxItem(或 ListViewItem)功能。 http://thrash505.wordpress.com/2011/01/04/non-selectable-listboxitem-or-listviewitem-using-attached-properties/
回答by Adrian Bystrek
You can also make disabled Listbox, which will give you static, non-interactive listbox.
您还可以制作禁用的列表框,这将为您提供静态的、非交互式的列表框。
<ListBox IsEnabled="False"/>
I think this is the solution as simple as possible.
我认为这是尽可能简单的解决方案。