【问题标题】:Controlling ListBox scrolling behaviour控制 ListBox 滚动行为
【发布时间】:2012-05-05 14:54:09
【问题描述】:

Adam Nathan 撰写的“WPF 4 Unleashed”的第 10 章包含这个控制 ListBox 滚动行为的 XAML 示例:

<Window x:Class="NathanControllingScrollingBehavior.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ScrollViewer.VerticalScrollBarVisibility="Disabled"
             ScrollViewer.CanContentScroll="False"
             ScrollViewer.IsDeferredScrollingEnabled="True">
        ...
    </ListBox>

</Window>

本书中没有等效的 C# 示例。我做了一些研究,发现了一些迂回的方法来做到这一点。请参阅this SO question 了解两种对我有用的方法。然而,这些方法似乎有点骇人听闻。在 XAML 中调整这些属性非常简单,但在 C# 中却很麻烦。

这仅仅是 WPF 的一个区域,它只能用于 XAML 而不是 C#?谁能解释 XAML/C# 之间这些属性的易用性差异?这只是 WPF 团队的疏忽吗?

【问题讨论】:

    标签: c# wpf xaml listbox scrollviewer


    【解决方案1】:

    你可以这样做。

    ListBox listBox1 = new ListBox();
    
    listBox1.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty,
    ScrollBarVisibility.Disabled);
    
    listBox1.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, 
    ScrollBarVisibility.Disabled);
    
    listBox1.SetValue(ScrollViewer.CanContentScrollProperty, false);
    
    listBox1.SetValue(ScrollViewer.IsDeferredScrollingEnabledProperty, true);
    

    【讨论】:

    • 谢谢尼基尔!这是完美的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    相关资源
    最近更新 更多