WPF:根据 ScrollBar 是否可见更改 ListBox ItemTemplate

发布于 2024-11-27 03:14:10 字数 788 浏览 4 评论 0原文

我为我的英语感到抱歉。


我需要根据垂直滚动条是否可见(或启用或禁用)来更改列表框中项目的数据模板。 我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2024-12-04 03:14:10

ScrollViewer 有两个属性:ComputedHorizo​​ntalScrollBarVisibility 和 ComputedVerticalScrollBarVisibility,它们是只读依赖属性,我们可以在 ListBox 的 ControlTemplate 的触发器中使用它们(这里我只考虑垂直属性)

<Style x:Key="StyleListBoxChangingItemTemplate" TargetType="{x:Type ListBox}">
    <Setter Property="ItemTemplate" Value="{StaticResource SomeItemTemplate}"/>
    <Setter Property="Template">
        <ControlTemplate TargetType="{x:Type ListBox}">
            <ScrollViewer x:Name="ListScroller">
                <ItemsPresenter />
            </ScrollViewer>
            <ControlTemplate.Triggers>
                <Trigger SourceName="ListScroller" Property="ComputedVerticalScrollBarVisibility" Value="Visible">
                    <Setter Property="ItemTemplate" Value="{StaticResource SomeOtherItemTemplate}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter>
</Style>

注意:为了清楚起见,这是一个精简的列表框模板。我删除了应该环绕 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)

<Style x:Key="StyleListBoxChangingItemTemplate" TargetType="{x:Type ListBox}">
    <Setter Property="ItemTemplate" Value="{StaticResource SomeItemTemplate}"/>
    <Setter Property="Template">
        <ControlTemplate TargetType="{x:Type ListBox}">
            <ScrollViewer x:Name="ListScroller">
                <ItemsPresenter />
            </ScrollViewer>
            <ControlTemplate.Triggers>
                <Trigger SourceName="ListScroller" Property="ComputedVerticalScrollBarVisibility" Value="Visible">
                    <Setter Property="ItemTemplate" Value="{StaticResource SomeOtherItemTemplate}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter>
</Style>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文