WPF列表框你能让它循环吗?(顶部和底部没有用力停止)
我有一个绑定到数据对象的WPF列表框。列表框中有一系列带有文本的图像。它以水平方式进行布局,并在框的左侧或右侧上鼠标分别向左或向右滚动项目 假设列表框中有20个项目。我试图弄清楚,当我点击位置19项(以0为基础)时,我如何循环框并重新开始收集,使其变为1-19,以此类推。它还需要以另一种方式循环,这样,如果您在项目0上,并向左滚动,您将得到19 我试过键盘导航。DirectionalNavigation=“Cycle”但这似乎对我没有任何帮助,我一直在抓救命稻草,因为这与键盘无关,它都是基于鼠标的WPF列表框你能让它循环吗?(顶部和底部没有用力停止),wpf,listbox,collections,cycle,databound,Wpf,Listbox,Collections,Cycle,Databound,我有一个绑定到数据对象的WPF列表框。列表框中有一系列带有文本的图像。它以水平方式进行布局,并在框的左侧或右侧上鼠标分别向左或向右滚动项目 假设列表框中有20个项目。我试图弄清楚,当我点击位置19项(以0为基础)时,我如何循环框并重新开始收集,使其变为1-19,以此类推。它还需要以另一种方式循环,这样,如果您在项目0上,并向左滚动,您将得到19 我试过键盘导航。DirectionalNavigation=“Cycle”但这似乎对我没有任何帮助,我一直在抓救命稻草,因为这与键盘无关,它都是基于鼠标
<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>
有许多免费的商业WPF carousel实现可以做到这一点。看看这篇综述有许多免费的商业WPF转盘实现可以实现这一点。看看这篇综述这里的问题并不是真正的列表框,而是它的控件模板中的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();
}
}
}
滚动到。。。和线。。。方法已经存在于ScrollViewer上,使得编码非常简单。我在这里所做的就是在滚动之前检查当前偏移量与查看器的边界
下一步是将新的ScrollViewer插入目标控件的控件模板,在本例中是列表框。这里有一个XAML片段来演示这一点
<ControlTemplate x:Key="{x:Type ListBox}" TargetType="ListBox">
...
<l:CyclicScrollViewer
Padding="{TemplateBinding Control.Padding}"
Focusable="False">
<ItemsPresenter ... />
</l:CyclicScrollViewer>
...
</ControlTemplate>
...
...
我发布了一个使用此代码的示例应用程序。此示例没有键盘支持,但可以通过覆盖OnKeyDown方法并执行适当的行来简单地添加它。。。命令。我希望这会有所帮助。这里的问题不是关于列表框的问题,而是关于控件模板中的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();
}
}
}
滚动到。。。和线。。。方法已经存在于ScrollViewer上,使得编码非常简单。我在这里所做的就是在滚动之前检查当前偏移量与查看器的边界
下一步是将新的ScrollViewer插入目标控件的控件模板,在本例中是列表框。这里有一个XAML片段来演示这一点
<ControlTemplate x:Key="{x:Type ListBox}" TargetType="ListBox">
...
<l:CyclicScrollViewer
Padding="{TemplateBinding Control.Padding}"
Focusable="False">
<ItemsPresenter ... />
</l:CyclicScrollViewer>
...
</ControlTemplate>
...
...
我发布了一个使用此代码的示例应用程序。此示例没有键盘支持,但可以通过覆盖OnKeyDown方法并执行适当的行来简单地添加它。。。命令。我希望这能有所帮助。我认为这些都很蹩脚。有没有这样做过。。。我刚才看到那个帖子了。好的是美元。而这并不是我想要达到的目标。谢谢。我觉得这些都很蹩脚。有没有这样做过。。。我刚才看到那个帖子了。好的是美元。而这并不是我想要达到的目标。谢谢。这类回答了我的问题,我的问题是我没有像你那样滚动列表,我不认为我能做我想做的,但我会标记它已被回答,因为我认为这是一个创造性的解决方案。:)这类回答了我的问题,我的问题是我没有像你那样滚动列表,我不认为我能做我想做的事,但我会标记它已被回答,因为我认为这是一个创造性的解决方案。:)FWIW使用
KeyboardNavigation.DirectionalNavigation=“Cycle”
在带有ListBoxItems的简单列表框上使用KeyboardNavigation.DirectionalNavigation=“Cycle”
在带有ListBoxItems的简单列表框上使用对我有效