ListBox中的滚动效果
这是此问题的后续内容
Increase FontSize for hovered element in WPF ListBox
我有一个列表框,我想要一个“滚动”效果。从一开始,ListBox 中的所有项目的 FontSize 均为 12,当 IsMouseOver="True" 时,它们的 FontSize=18。现在我希望 IsMouseOvered 项目下方和上方的项目获得 FontSize 16。我有一种感觉,这并不是那么简单。有人知道吗?
这是我的列表框,
<ListBox Name="ListBox" ItemsSource="{Binding MyList}" DisplayMemberPath="Property1">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextBlock.FontSize" Value="18"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
谢谢
This is a follow-up to this question
Increase FontSize for hovered element in WPF ListBox
I have a ListBox where I want a "rolling" effect. From start all my items in the ListBox have a FontSize of 12, and when IsMouseOver="True" they get FontSize=18. Now I want the item below and above the IsMouseOvered item to get FontSize 16. I've got a feeling this is not so straight forward. Anyone got any idea?
This is my ListBox now
<ListBox Name="ListBox" ItemsSource="{Binding MyList}" DisplayMemberPath="Property1">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextBlock.FontSize" Value="18"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这在直接 XAML 中很难完成,因为没有任何属性可以让您检查来告诉您鼠标当前是否位于列表中的下一个(或上一个)元素上。
我实现此方法的方式:我将为列表中的项目创建一个包装类,该包装类公开属性
IsMouseOver
、FontSize
和Content.我将实现一个父类,它维护这些项目的某种可索引集合(例如数组或列表),并注册其项目的
PropertyChanged
事件,以便它可以知道其中的哪个项目鼠标当前悬停的列表。然后,每当当前鼠标悬停的项目发生变化时,父类将负责调整子对象上的FontSize
。 XAML 所做的全部工作就是绑定到子对象上的FontSize
。顺便说一句,请注意,如果您实现某种很酷的效果(例如 Aaron 链接到的示例中的效果,顺便说一句,它没有达到您所说的效果),则在
IsMouseOver 时调整元素大小
发生变化,在屏幕上移动它们,使得鼠标所在的元素也会发生变化,您的用户会在您睡梦中追捕您并杀死您。This is going to be difficult to accomplish in straight XAML, because there's no property that you can examine that tells you if the mouse is currently over the next (or previous) element in the list.
The way I'd implement this: I'd create a wrapper class for the items in your list that exposes the properties
IsMouseOver
,FontSize
, andContent
. I'd implement a parent class that maintains some kind of indexable collection of these items (like an array or a list), and that registers for its items'PropertyChanged
events so that it can know which item in the list the mouse is presently hovering over. The parent class would then be responsible for adjusting theFontSize
on the child objects whenever the currently moused-over item changes. All the XAML does is bind to theFontSize
on the child objects.Note, by the way, that if you implement some kind of cool effect (like the one in the example Aaron linked to, which incidentally doesn't do what you've said you want) that, in resizing elements when
IsMouseOver
changes, moves them on the screen in such a way that the element that the mouse is over changes too, your users will hunt you down and kill you in your sleep.看来您想要鱼眼效果。 WPF 有 各种解决方案,它们以以下形式为您提供此行为例如,可重复使用的面板。他们通常关注图像,但概念是相同的。此解决方案显示了一个示例在纯 XAML 中。
It appears you are going for the fish eye effect. There are varying solutions out there for WPF which provide this behavior for you in the form of a reusable panel for instance. They generally focus on images however the concept is the same. This solution shows an example in pure XAML.
我写了这个用户控件来模仿Mac的仪表板(鱼眼效果)。
I wrote this user control that would mimic the dashboard of the Mac's (Fish eye effect).