我有一个ItemsControl数据列表,我想虚拟化,但VirtualizingStackPanel.IsVirtualizing="True"似乎不适用于ItemsControl.
这是真的吗,还是有另一种方法可以做到这一点,我不知道?
要测试我一直在使用以下代码块:
<ItemsControl ItemsSource="{Binding Path=AccountViews.Tables[0]}"
VirtualizingStackPanel.IsVirtualizing="True">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Initialized="TextBlock_Initialized"
Margin="5,50,5,50" Text="{Binding Path=Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
如果我将其更改ItemsControl为a ListBox,我可以看到该Initialized事件只运行了几次(巨大的边距只是因此我只需要通过一些记录),但是ItemsControl每个项目都会被初始化.
我试过设置ItemsControlPanelTemplate为a VirtualizingStackPanel但似乎没有帮助.
我之前从未注意到这一点,但是当鼠标停止时,WPF ListBox似乎更改了其SelectedItem,但尚未发布.作为一个简单的例子,只需创建一个包含多个ListBoxItem的简单ListBox,如下所示:
<ListBox>
<ListBoxItem>Hello</ListBoxItem>
<ListBoxItem>World</ListBoxItem>
<ListBoxItem>ListBox</ListBoxItem>
<ListBoxItem>Test</ListBoxItem>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
启动你的应用程序,按下鼠标按钮(不要释放它!)并移动鼠标.SelectedItem将随着鼠标移动而改变.这说明了一个更大的问题(至少对我来说),一旦你按下鼠标就会设置ListBox的SelectedItem ,而不是在鼠标启动时设置.通常这不是问题,但在我的情况下,我想在我的ListBox中的项目上进行拖放操作,而不会明确选择项目.
我想我唯一的办法就是构建一个自定义的ItemsControl或Selector,其选择风格类似于ListBox,所以我的问题更多,为什么ListBox以这种方式工作?有没有人对此有任何见解?
整个上午都在撞我的头.
基本上,我有一个列表框,我想让人们不要在长时间运行的过程中更改选择,但允许他们仍然滚动.
解:
所有答案都很好,我选择了吞咽老鼠事件,因为那是最直接的.我将PreviewMouseDown和PreviewMouseUp连接到单个事件,该事件检查了我的backgroundWorker.IsBusy,如果将事件args的IsHandled属性设置为true.
我正在开发的应用程序有一种由扩展器组成的手风琴,但每个扩展器独立运行,而不是一次只允许一个打开的项目.每个扩展器都与视图模型中集合中的项相关.
我目前的解决方案是使用列表框,然后将列表itemsource绑定到集合,并使项目模板呈现扩展器和扩展器内容.
问题是列表框将每个扩展器视为一个项目(显然),并允许选择和突出显示.突出显示有点难看,可能会被禁用,但选择会导致一些问题,因为它会导致列表滚动显示尽可能多的扩展扩展器.
是否有一个WPF控件有点像stackpanel(可能),它允许我使用项目模板绑定包含的控件但没有选择和突出显示?

<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>
Run Code Online (Sandbox Code Playgroud) 是否有一个很好的方法(除了重新整理TreeViewItem.Template)以禁用选择TreeView?
我基本上寻找的ItemsControl风格TreeView(An ItemsControl是最好用'禁用'选择ListBox,阅读这篇文章)
如何禁用Listview中元素的选择?
当我在ListView中单击elem时,我不想更改背景.你能帮助我吗?
<ListView Name="milestone_listView" Margin="817,108,90,276" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" ItemsSource="{Binding}">
<Grid Name="milestone_grid"></Grid>
</ListView>
Run Code Online (Sandbox Code Playgroud)