【发布时间】:2013-11-18 10:07:00
【问题描述】:
我的 ListBox 的 ControlTemplate 中有 ScrollViewer,我正在使用 .Net 4.0 我在 CanContentScroll 的不同值上面临两个问题。
-
当 CanContentScroll 为 True 时。
在这种情况下,当我用鼠标滚动 ListBox 时,ScrollViewer 似乎会随机跳过视图中的 1 或 2 个项目。但 SelectedItem 的选择工作正常。
-
当 CanContentScroll 为 False 时。
在这种情况下,使用鼠标滚动可以正常工作。但是现在 SelectedItem 的选择并没有选择实际的 SelectedItem,而是选择它上面或下面的项目。或继续显示之前的 SelectedItem。
请帮帮我。
<Style TargetType="ListBox" x:Key="ListBoxStyle">
<Setter Property="ItemContainerStyle" Value="{StaticResource ItemContainerStyle}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer CanContentScroll="True">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ItemContainerSTyle:
<Style x:Key="ItemContainerStyle" BasedOn="{StaticResource listBoxItemStyle}" TargetType="ListBoxItem">
<Setter Property="helper:FluidSelectionAttachedPropertyHelper.IgnoreRightClick" Value="True"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger >
</Style.Triggers>
</Style>
项目模板:
<Style TargetType="ListBoxItem" x:Key="listBoxItemStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter Margin="2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【问题讨论】:
-
您有任何代码/xaml 与我们分享。接口很难调试。
-
我添加了我正在使用的样式。
-
添加您的 itemcontainer 样式和 ListBoxItem 模板和 ListBox.ItemTemplate 如果存在。
标签: wpf xaml scrollviewer