【发布时间】:2010-12-15 21:35:33
【问题描述】:
我在 WPF 中有一个列表框,当他们选择一个项目时,它显示出难看的颜色 我可以将所有项目都设为不可选择吗?
【问题讨论】:
-
订购东西永远不会太晚
-
在列表框上设置 Enabled="false"。不透明度可以通过 CSS 调整。
我在 WPF 中有一个列表框,当他们选择一个项目时,它显示出难看的颜色 我可以将所有项目都设为不可选择吗?
【问题讨论】:
如果您不需要选择,请使用ItemsControl 而不是ListBox
【讨论】:
ItemsControl 不能做一些 ListBox 可能需要的事情,例如在使用虚拟化时 ScrollIntoView。
在 ListBoxItem 样式中将 Focusable 属性添加为 false:
<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}">
<!-- Possibly other setters -->
<Setter Property="Focusable" Value="False" />
</Style>
【讨论】:
请在您的列表框中使用它。我发现了这个非常优雅的解决方案
<ListBox ItemsSource="{Binding YourCollection}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
【讨论】:
如果您不希望它们可选择,那么您可能不想要列表视图。 但如果这是你真正需要的,那么你可以用一种风格来做到这一点:
<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 触发器。您可以将边框设置为不同的颜色,使其不“丑”或将其设置为透明,并且在选中时不可见。
希望这会有所帮助。
【讨论】:
还有一个更简单的方法:设置ListBox 属性IsHitTestVisible="False"。这可以防止列表中的所有项目接收鼠标事件。这具有在鼠标悬停时停止突出显示的优点。
它在 WP 7.1 中适用于我。
【讨论】:
ListBox 周围添加自己的 ScrollViewer 以重新建立滚动。
执行此操作的一种简单方法(使用上面 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;
}
}
【讨论】:
最好避免事件,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>
【讨论】:
您可以处理 ListBox 的 SelectionChanged 事件,并在事件处理程序中取消选择所选项目。
【讨论】:
您还可以禁用列表框,这将为您提供静态、非交互式列表框。
<ListBox IsEnabled="False"/>
我认为这是尽可能简单的解决方案。
【讨论】:
在我的例子中,我使用一个文本块和一个组合框来模板化 ListboxItems。唯一的“活跃”应该是 Combo...
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled"
CanContentScroll="True" />
<ItemsControl>
....here my content....
</Itemscontrol>
</ScrollViewer>
确实按预期为我工作。 BR, 丹尼尔
【讨论】:
你也可以处理 PreviewMouseDown 事件
为了防止点击,您可以设置KeyboardNavigation.TabNavigation="None"
<ListView x:Name="Cards"
.....
PreviewMouseDown="CardMonthsDescriptors_OnPreviewMouseDown"
KeyboardNavigation.TabNavigation="None"
>
...
private void Cards_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
【讨论】: