如何启用鼠标水平滚动?
我无法确定如何使用鼠标滚轮水平滚动。垂直滚动可以自动很好地工作,但我需要水平滚动我的内容。我的代码如下所示:
<ListBox x:Name="receiptList"
Margin="5,0"
Grid.Row="1"
ItemTemplate="{StaticResource receiptListItemDataTemplate}"
ItemsSource="{Binding OpenReceipts}"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"
ScrollViewer.HorizontalScrollBarVisibility="Visible"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListBox>
我的项目模板如下所示:
<DataTemplate x:Key="receiptListItemDataTemplate">
<RadioButton GroupName="Numbers"
Command="{Binding Path=DataContext.SelectReceiptCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type POS:PointOfSaleControl}}}"
CommandParameter="{Binding }"
Margin="2,0"
IsChecked="{Binding IsSelected}">
<RadioButton.Template>
<ControlTemplate TargetType="{x:Type RadioButton}" >
<Grid x:Name="receiptGrid" >
<Grid>
<Border BorderThickness="2"
BorderBrush="Green"
Height="20"
Width="20">
<Grid x:Name="radioButtonGrid"
Background="DarkOrange">
<TextBlock x:Name="receiptLabel"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Path=NumberInQueue, Mode=OneWay}"
FontWeight="Bold"
FontSize="12"
Foreground="White">
</TextBlock>
</Grid>
</Border>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Margin"
TargetName="receiptGrid"
Value="2,2,-1,-1"/>
<Setter Property="Background"
TargetName="radioButtonGrid"
Value="Maroon"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</RadioButton.Template>
</RadioButton>
</DataTemplate>
我需要添加其他方法或控件才能获得该功能吗?
I cannot determine how to scroll horizontally using the mouse wheel. Vertical scrolling works well automatically, but I need to scroll my content horizontally. My code looks like this:
<ListBox x:Name="receiptList"
Margin="5,0"
Grid.Row="1"
ItemTemplate="{StaticResource receiptListItemDataTemplate}"
ItemsSource="{Binding OpenReceipts}"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"
ScrollViewer.HorizontalScrollBarVisibility="Visible"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListBox>
My item template looks like this:
<DataTemplate x:Key="receiptListItemDataTemplate">
<RadioButton GroupName="Numbers"
Command="{Binding Path=DataContext.SelectReceiptCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type POS:PointOfSaleControl}}}"
CommandParameter="{Binding }"
Margin="2,0"
IsChecked="{Binding IsSelected}">
<RadioButton.Template>
<ControlTemplate TargetType="{x:Type RadioButton}" >
<Grid x:Name="receiptGrid" >
<Grid>
<Border BorderThickness="2"
BorderBrush="Green"
Height="20"
Width="20">
<Grid x:Name="radioButtonGrid"
Background="DarkOrange">
<TextBlock x:Name="receiptLabel"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Path=NumberInQueue, Mode=OneWay}"
FontWeight="Bold"
FontSize="12"
Foreground="White">
</TextBlock>
</Grid>
</Border>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Margin"
TargetName="receiptGrid"
Value="2,2,-1,-1"/>
<Setter Property="Background"
TargetName="radioButtonGrid"
Value="Maroon"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</RadioButton.Template>
</RadioButton>
</DataTemplate>
Is there another method or control that I need to add to get that functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个:
更新 哎呀,错过了关于鼠标滚轮的部分!抱歉,
要使鼠标滚轮工作,您必须订阅鼠标滚轮事件并手动移动滚动条...这可以很好地封装为行为,但我认为这是使其工作的唯一方法!
Try this:
UPDATE Ooops, missed the part about the mouse wheel! Sorry
To make the mouse wheel work, you will have to subscribe to the mouse wheel event and manuaaly move the scroll bar... This can nicelly be encapsulated by a behavior but I think thats the only way to make it work!
这是一个完整的行为。将以下类添加到您的代码中,然后在 XAML 中将任何包含
ScrollViewer
作为可视子项的UIElement
的附加属性设置为 true。课程:
此答案修复了 Johannes 的答案 中的一些错误,例如不按 Shift 键进行过滤、水平滚动和同时垂直(运动是对角线)并且无法通过将属性设置为 false 来禁用该行为。
Here's a complete behaviour. Add the below class to your code, then in your XAML set the attached property to true on any
UIElement
that contains aScrollViewer
as a visual child.The class:
This answer fixes a few bugs in Johannes' answer, such as not filtering by Shift key, scrolling both horizontally and vertically at the same time (motion was diagonal) and the inability to disable the behaviour by setting the property to false.
为此,我编写了一个
附加属性
,以便在每个包含 ScrollViewer 的ItemsControl
上重用它。FindChildByType
是 Telerik 扩展,但也可以在 此处。I wrote an
Attached Property
for this purpose to reuse it on everyItemsControl
containing an ScrollViewer.FindChildByType
is an Telerik extension but can also be found here.我有点寻找最简单的方法来使任何
ScrollViewer
左右滚动而不是上下滚动。所以这是其他答案的最简单组合。并且:
这基本上就是 John Gardner 建议的实际代码。我还引用了我在类似问题中的回答。
I was kinda looking for the most simple way to make any
ScrollViewer
scroll left-right instead of up-down. So here is the simplest combination of the other answers.and:
This is basically what John Gardner suggested just with the actual code. I'm also quoting my answer from similar question here.
最简单的方法是将
PreviewMouseWheel
侦听器添加到ScrollViewer
,检查是否有平移(或任何您想要指示水平滚动的操作),然后调用LineLeft< /code> 或
LineRight
(或PageLeft
/PageRight
),具体取决于Delta
值的值代码>MouseWheelEventArgsThe easiest way is to add
PreviewMouseWheel
listener to theScrollViewer
, check for shift (or whatever you want to do to indicate horizontal scroll), and then callLineLeft
orLineRight
(orPageLeft
/PageRight
) depending on the value of theDelta
value of theMouseWheelEventArgs