簡體   English   中英

始終在ScrollViewer滾動條中顯示拇指

[英]Always show thumb in ScrollViewer scrollbar

如果ScrollViewer的內容小於可用區域,如何使ScrollViewer控件的滾動條始終顯示滾動條的大拇指,並且大拇指最大長度?

使用以下XAML:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication8"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="200">
    <ScrollViewer HorizontalScrollBarVisibility="Visible" 
                  VerticalScrollBarVisibility="Visible">
        <Grid Height="300" Width="300" Background="Red">

        </Grid>
    </ScrollViewer>
</Window>

將窗口調整為小於300x300像素網格的大小將使水平和垂直滾動條中的拇指出現。

但是,當將窗口大小增加到大於300x300像素網格時,水平和垂直滾動條便會消失。

總是顯示滾動條的拇指

一種可能性是訂閱Track (即您單擊並隱藏的部分) IsVisibilityChanged事件並強制顯示它:

HackTheTrack(scrollViewer.FindChildren<Track>().First());
HackTheTrack(scrollViewer.FindChildren<Track>().Skip(1).Single()); // second track

void HackTheTrack(Track track)
{
    track.Visibility = Visibility.Visible;
    track.IsVisibleChanged += (s, ee) =>
    {
        if (track.Visibility != Visibility.Visible)
            track.Visibility = Visibility.Visible;
    };
}

您將需要以下擴展方法:

public static IEnumerable<T> FindChildren<T>(this DependencyObject @this) where T : DependencyObject
{
    if (@this == null)
        yield break;
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(@this); i++)
    {
        var child = VisualTreeHelper.GetChild(@this, i);
        var result = child as T;
        if (result != null)
            yield return result;
        foreach (var value in FindChildren<T>(child))
            yield return value;
    }
    yield break;
}

但這看起來很混亂:



暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM