WPF ListBox中你能来循环? IE不打硬在顶部和底部停止
-
23-08-2019 - |
题
我有绑定到的数据对象的列表框WPF。列表框里面是一系列的文本图像。它是在以水平方式奠定出来,鼠标悬停在所述框的左或右侧滚动项目向左或向右分别
让我们说有ListBox中的20个项目。我试图找出如何时,我打19位的项目(基于0),我可以循环盒子,重新开始集合,使其变为1 -19等。那就需要也循环的其他方式,因此,如果你是在项目0,滚动离开,你会得到19
我试过KeyboardNavigation.DirectionalNavigation =“循环”,但是,这并不”似乎为我做什么,我是在抓救命稻草,因为这无关的键盘,它是一个基于所有的鼠标。
<ListBox ItemsSource="{Binding Source={StaticResource WPFApparelCollection}}" Margin="24,-7,39,-19" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Hidden" SelectionMode="Single" x:Name="list1" MouseLeave="List1_MouseLeave" MouseMove="List1_MouseMove" Style="{DynamicResource ListBoxStyle1}" Background="Transparent" BorderThickness="0">
<ListBox.Resources>
<!-- override the system brushes so that selected items are transparent whether the ListBox has focus or not -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="Padding" Value="20,10,20,10" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="Transparent" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Image Source="{Binding Image}" MouseLeave="Image_MouseLeave" MouseEnter="Image_MouseEnter" Cursor="Hand" Tag="{Binding Link}" MouseLeftButtonDown="Image_MouseLeftButtonDown" VerticalAlignment="Top" HorizontalAlignment="Left"></Image>
<Label Content="{Binding Name}" Cursor="Hand" Tag="{Binding Link}" MouseLeftButtonDown="Label_MouseLeftButtonDown" VerticalAlignment="Bottom" Foreground="White" Style="{StaticResource Gotham-Medium}" FontSize="8pt" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
解决方案
这里的问题是不是真的与的ListBox ,但与的ScrollViewer 的控件模板;因此,使项目周期你需要改变的的ScrollViewer 以某种方式。我写了从派生的控件的的ScrollViewer ,在垂直方向上的循环...但它应该很容易看到如何使水平过于它的工作。
public class CyclicScrollViewer : ScrollViewer
{
public CyclicScrollViewer()
{
this.CommandBindings.Add(new CommandBinding(ScrollBar.LineUpCommand, LineCommandExecuted));
this.CommandBindings.Add(new CommandBinding(ScrollBar.LineDownCommand, LineCommandExecuted));
}
private void LineCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == ScrollBar.LineUpCommand)
{
if (this.VerticalOffset == 0)
this.ScrollToEnd();
else
this.LineUp();
}
if (e.Command == ScrollBar.LineDownCommand)
{
if (this.VerticalOffset == this.ScrollableHeight)
this.ScrollToTop();
else
this.LineDown();
}
}
}
在 ScrollTo ... 的和的行... 的方法已经存在的的ScrollViewer 使得编码非常简单。所有我在这里做的是检查之前滚动当前对观众的界限偏移。
下一步是插入新的的ScrollViewer 强>成用于目标控制的控制模板,在这种情况下的列表框即可。这里的一个XAML片段来证明这一点。
<ControlTemplate x:Key="{x:Type ListBox}" TargetType="ListBox">
...
<l:CyclicScrollViewer
Padding="{TemplateBinding Control.Padding}"
Focusable="False">
<ItemsPresenter ... />
</l:CyclicScrollViewer>
...
</ControlTemplate>
我已使用该代码的示例程序 这里 强> 。该样品不具有键盘的支持,但可以通过重写的的onkeydown 方式并执行适当的线... 的命令被简单地添加。我希望这有助于。
其他提示
有许多免费和商业WPF转盘实现,做到这一点。看看这篇综述 http://mdavey.wordpress.com/2007 / 04/03 / WPF的圆盘传送带/
不隶属于 StackOverflow