是否有没有选择的 WPF 列表视图之类的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17852452/
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
Is there something like a WPF listview without the selection?
提问by naskew
The application I'm developing has a kind of accordion made of expanders but each exapnder acts independently rather than only allowing a single open item at a time. Each expander is related to an item in a collection in the view model.
我正在开发的应用程序有一种由扩展器组成的手风琴,但每个扩展器都独立运行,而不是一次只允许一个打开的项目。每个扩展器都与视图模型中集合中的一个项目相关。
My currently solution is to use a listbox and then bind the lists itemsource to the collection and have an item template render the expander and the exapnders contents.
我目前的解决方案是使用列表框,然后将列表 itemsource 绑定到集合,并让项目模板呈现扩展器和扩展器内容。
The problem is that the listbox treats each expander as an item (obviously) and allows selection and highlighting. Highlighting is a little ugly and could be disabled, but the selection causes me some issues because it causes the list to scroll to show as much of the expanded expander as possible.
问题是列表框将每个扩展器视为一个项目(显然)并允许选择和突出显示。突出显示有点难看,可以禁用,但选择会导致我一些问题,因为它会导致列表滚动以显示尽可能多的扩展扩展器。
Is there a WPF control that is a little like a stackpanel (perhaps) that would allow me to bind the contained controls using item templating but without the selection and highlighting?
是否有一个有点像堆栈面板(也许)的 WPF 控件,它允许我使用项目模板绑定包含的控件但没有选择和突出显示?


<ListBox Grid.Row="0" Grid.Column="0" Width="Auto" SelectionMode="Single" ItemsSource="{Binding Path=MeasurementSources}">
<ListBox.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Name}" IsEnabled="{Binding Available}">
<ListBox Width="Auto" SelectionMode="Single"
ItemsSource="{Binding Path=Measurements}"
SelectedItem="{Binding Path=SelectedMeasurement}">
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text=" "/>
<TextBlock Text="{Binding Created}"/>
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
回答by flq
If you want List-like capabilities without selection capabilities, you should use an ItemsControl- incidentally the base class of Selectorwhich in turn is the base class of ListBox
如果您想要没有选择功能的类似列表的功能,您应该使用ItemsControl- 顺便说一句,Selector它的基类又是ListBox
The whole thing then just becomes
整个事情就变成了
<ItemsControl Width="Auto" ItemsSource="{Binding Measurements}"
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text=" "/>
<TextBlock Text="{Binding Created}"/>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Obviously, you cannot bind a selected item in this case.
显然,在这种情况下您不能绑定选定的项目。
回答by Mateusz Dembski
The best solution is for me:
对我来说最好的解决方案是:
<Style TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<!-- With focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<!-- Without focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Transparent" />
<!-- Text foreground -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black" />
</Style.Resources>
<Setter Property="FocusVisualStyle" Value="{x:Null}"></Setter>
</Style>
and I found it few months ago somwhere on stackoverflow:)
几个月前我在某处找到了它stackoverflow:)
回答by naskew
This is a useful piece of information that I had not realised.
这是我没有意识到的有用信息。
http://msdn.microsoft.com/library/windows/apps/hh780657.aspx
http://msdn.microsoft.com/library/windows/apps/hh780657.aspx
Note ItemsControl is the base class for several common collection controls, including ListView, GridView, FlipView, ListBox, and ComboBox controls. These examples use the ListView and GridView controls, but the info applies generally to ItemsControls.
注意 ItemsControl 是几个常用集合控件的基类,包括 ListView、GridView、FlipView、ListBox 和 ComboBox 控件。这些示例使用 ListView 和 GridView 控件,但这些信息通常适用于 ItemsControl。
In other words using the ItemsControl really is the way to solve my issue because using a ListBox and then trying to disable the highlight and the selection functionality really is turning it back into it's own base class.
换句话说,使用 ItemsControl 确实是解决我的问题的方法,因为使用 ListBox 然后尝试禁用突出显示和选择功能确实将其转回它自己的基类。

