WPF:根据 ScrollBar 是否可见更改 ListBox ItemTemplate
我为我的英语感到抱歉。
我需要根据垂直滚动条是否可见(或启用或禁用)来更改列表框中项目的数据模板。 我使用 ListBox 和 ScrollBar 的样式。 当其属性“IsEnabled”的值为“False”时,我可以更改滚动条模板。但我不明白如何在 ListBox 样式中捕获 ScrollBar.VisibilityChanging 。我尝试使用
<Style TargetType="{x:Type ListBox}" >
.....
<Style.Triggers>
<Trigger Property="ScrollViewer.ComputedVerticalScrollBarVisibility"
Value="Hidden">
<Setter Property="ItemTemplate">
......
...with...
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
..........
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Hidden" />
</Trigger>
......
这是行不通的。
我希望你能帮助我
I'm sorry for my English.
I need to change the DataTemplate for items in a ListBox depending on whether the Vertical ScrollBar is visible or not (or enabled or disabled).
I use styles for ListBox and ScrollBar.
I can change scrollBar template when its property "IsEnabled" has value "False". But I can't understand how to catch ScrollBar.VisibilityChanging inside ListBox Style. I tryed to use
<Style TargetType="{x:Type ListBox}" >
.....
<Style.Triggers>
<Trigger Property="ScrollViewer.ComputedVerticalScrollBarVisibility"
Value="Hidden">
<Setter Property="ItemTemplate">
......
...with...
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
..........
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Hidden" />
</Trigger>
......
It is not work.
I hope you help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ScrollViewer 有两个属性:ComputedHorizontalScrollBarVisibility 和 ComputedVerticalScrollBarVisibility,它们是只读依赖属性,我们可以在 ListBox 的 ControlTemplate 的触发器中使用它们(这里我只考虑垂直属性)
注意:为了清楚起见,这是一个精简的列表框模板。我删除了应该环绕 ScrollViewer 的边框以及在 ScrollViewer 上定义的所有属性。
The ScrollViewer has two properties: ComputedHorizontalScrollBarVisibility and ComputedVerticalScrollBarVisibility that are read-only dependency properties and we can use them in Triggers in the ControlTemplate of our ListBox (here I'm considering only the vertical property)
NOTE: for clarity of the answer, this is a stripped-down, bare-bones template for a ListBox. I removed the Border that should wrap around the ScrollViewer and all the properties that are defined on the ScrollViewer.