设置滚动条缩略图大小

时间:2010-06-25 07:32:48

标签: wpf slider scrollbar

我正在尝试找出与WPF Scrollbar thumb元素大小相关的算法。

可以使用Scrollbar.ViewportSize属性调整thumb元素的大小,但它又与Scrollbar.MinimumScrollbar.Maximum值相关。

到目前为止我发现的是:

对于 0 10 的最小值和最大值,ViewportSize为:

0 - 拇指最小尺寸
5 - 拇指大约25%的可用轨道
10 - 拇指大约50%的可用轨道
100 - 拇指大约75%的可用轨道
1000 - 拇指大约90%的可用轨道
10000 - Thumb填充可用曲目。

[注意:这些数字仅来自我粗略的试错!]

理想情况下,我希望能够有一个算法,在给定滚动条的最小值和最大值的情况下,我可以将拇指大小设置为可用轨道的x%。

任何人都可以帮忙吗?

感谢。

5 个答案:

答案 0 :(得分:12)

来自:http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.track(VS.90).aspx

  

thumbSize =   (viewportSize /(最大值 - 最小值+ viewportSize))×径迹长度

或重新安排viewportSize:

  

viewportSize =   thumbSize×(最大 - 最小)/(径迹-thumbSize)

你已经发现了这个问题,但我认为我会发帖以防其他人到此为止。

答案 1 :(得分:4)

就我而言,我保留了最小拇指长度,因为触摸输入需要最小尺寸的拇指才能进行触摸优化。

您可以定义一个ScrollViewer ControlTemplate,它将使用TouchScrollBar作为其水平和垂直ScrollBar。

请参阅UpdateViewPort方法进行数学运算。

很抱歉,我没有看到明确设置滚动条拇指以覆盖轨道长度百分比的用例

public class TouchScrollBar : System.Windows.Controls.Primitives.ScrollBar
{
    #region Fields

    #region Dependency properties

    public static readonly DependencyProperty MinThumbLengthProperty =
        DependencyProperty.Register
        ("MinThumbLength", typeof(double), typeof(TouchScrollBar), new UIPropertyMetadata((double)0, OnMinThumbLengthPropertyChanged));

    #endregion

    private double? m_originalViewportSize;

    #endregion

    #region Properties

    public double MinThumbLength
    {
        get { return (double)GetValue(MinThumbLengthProperty); }
        set { SetValue(MinThumbLengthProperty, value); }
    }

    #endregion

    #region Constructors

    public TouchScrollBar()
    {
        SizeChanged += OnSizeChanged;
    }

    private bool m_trackSubscribed;
    void OnSizeChanged(object sender, SizeChangedEventArgs e)
    {
        SubscribeTrack();
    }

    private void SubscribeTrack()
    {
        if (!m_trackSubscribed && Track != null)
        {
            Track.SizeChanged += OnTrackSizeChanged;
            m_trackSubscribed = true;
        }

    }

    #endregion

    #region Protected and private methods

    #region Event handlers

    #region Dependency properties event handlers

    private void OnMinThumbLengthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TouchScrollBar instance = d as TouchScrollBar;
        if(instance != null)
        {
            instance.OnMinThumbLengthChanged(e);

        }
    }

    #endregion

    protected void OnTrackSizeChanged(object sender, SizeChangedEventArgs e)
    {
        SubscribeTrack();
        UpdateViewPort();
    }

    protected override void OnMaximumChanged(double oldMaximum, double newMaximum)
    {
        base.OnMaximumChanged(oldMaximum, newMaximum);

        SubscribeTrack();
        UpdateViewPort();
    }

    protected override void OnMinimumChanged(double oldMinimum, double newMinimum)
    {
        base.OnMinimumChanged(oldMinimum, newMinimum);

        SubscribeTrack();
        UpdateViewPort();
    }

    protected void OnMinThumbLengthChanged(DependencyPropertyChangedEventArgs e)
    {
        SubscribeTrack();
        UpdateViewPort();
    }

    #endregion

    private void UpdateViewPort()
    {
        if(Track != null)
        {
            if(m_originalViewportSize == null)
            {
                m_originalViewportSize = ViewportSize;
            }

            double trackLength = Orientation == Orientation.Vertical ? Track.ActualHeight : Track.ActualWidth;
            double thumbHeight = m_originalViewportSize.Value / (Maximum - Minimum + m_originalViewportSize.Value) * trackLength;
            if (thumbHeight < MinThumbLength && !double.IsNaN(thumbHeight))
            {
                ViewportSize = (MinThumbLength * (Maximum - Minimum)) / (trackLength + MinThumbLength);
            }
        }
    }


    #endregion
}

}

答案 2 :(得分:1)

如果您正在寻找如何设置滚动条拇指的最小高度:

来自伊恩(真正的MVP)here

[nginx error][1]


  [1]: https://i.stack.imgur.com/iwMhQ.png


Things I have tried:
-Yum Clean All

-Installed Epel-release 

-Tried to install apt-get,wget, and unzip but receive the same errors

- created a yum repo for nginx with the following:
[nginx]
name=nginx repo
baseurl= ht tp://n ginx.org /packages/centos/7/$basearch/
gpgcheck=0
enabled=1

- sudo yum -y install nginx httpd-tools

- sudo vi /etc/nginx/nginx.conf

或者你知道,添加100多行xaml代码导致omgDATABINDING !! 1!

答案 3 :(得分:0)

UWP的滚动条拇指大小:

    static void SetViewportSize(ScrollBar bar, double size)
    {
        var max = (bar.Maximum - bar.Minimum);
        bar.ViewportSize = size / (max - size) * max;
        bar.IsEnabled = (bar.ViewportSize >= 0 &&
            bar.ViewportSize != double.PositiveInfinity);
        InvalidateScrollBar(bar);
    }

    static void InvalidateScrollBar(ScrollBar bar)
    {
        var v = bar.Value;
        bar.Value = (bar.Value == bar.Maximum) ? bar.Minimum : bar.Maximum;
        bar.Value = v;
    }

答案 4 :(得分:0)

这是一种将覆盖所有ScrollBar的拇指最小宽度的方法。使用此设置有两个重要原因。

1)这不会调整ScrollBar RepeatButton的大小。 (为什么样式会覆盖Track

2)这将调整Track中使用的ScrollBar控件的拇指的大小。 (为什么Track样式包含在ScrollBar样式中。

<!-- Override for all styles -->
<Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource {x:Type ScrollBar}}">
    <Style.Resources>
        <Style TargetType="{x:Type Track}">
            <Style.Resources>
                <System:Double x:Key="{x:Static SystemParameters.VerticalScrollBarButtonHeightKey}">48</System:Double>
                <System:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}">48</System:Double>
            </Style.Resources>
        </Style>
    </Style.Resources>
</Style>

<!-- Override for a certain control -->
<!-- The ScrollBar Style part in the middle can be safely ommited
if you can guarantee the control only uses Tracks for ScrollBars -->
<SomeControl>
    <SomeControl.Resources>
        <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource {x:Type ScrollBar}}">
            <Style.Resources>
                <Style TargetType="{x:Type Track}">
                    <Style.Resources>
                        <System:Double x:Key="{x:Static SystemParameters.VerticalScrollBarButtonHeightKey}">48</System:Double>
                        <System:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}">48</System:Double>
                    </Style.Resources>
                </Style>
            </Style.Resources>
        </Style>
    </SomeControl.Resources>
</SomeControl>