wpf 设置滚动条缩略图大小

nzkunb0c  于 2023-03-19  发布在  其他
关注(0)|答案(5)|浏览(167)

我正试图找出与WPF滚动条缩略图元素的大小相关的算法。
thumb元素可以使用Scrollbar.ViewportSize属性来调整大小,但它又与Scrollbar.MinimumScrollbar.Maximum值相关。
到目前为止我发现的是:
对于010的最小值和最大值,视口大小为:
0 -拇指最小尺寸
5 -拇指大约25%的可用轨迹
10 -拇指大约50%的可用轨迹
100 -拇指大约75%的可用轨迹
1000 -拇指大约90%的可用轨迹
10000 -拇指填充可用轨道。
[note:这些数字只是我粗略试错的结果!]
理想情况下,我希望能够有一个算法,其中给定滚动条的最小值和最大值,我可以设置拇指大小正好是可用轨道的x%。
有人能帮忙吗?
谢谢。

fykwrbwg

fykwrbwg1#

出发地:http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.track(VS.90).aspx
缩略图大小=(视口大小/(最大值-最小值+视口大小))×轨迹长度
或重新排列视口大小:
视口大小=缩略图大小×(最大值-最小值)/(轨迹长度-缩略图大小)
你可能已经找到了这个,但认为我会张贴在其他人在这里结束。

ekqde3dh

ekqde3dh2#

在我这边,我保留了最小的拇指长度,因为触摸输入需要最小尺寸的拇指才能进行触摸优化。
您可以定义一个ScrollViewer控件模板,该模板将使用TouchScrollBar作为其水平和垂直滚动条。
有关数学计算,请参见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
}

}

cx6n0qe3

cx6n0qe33#

这里有一个方法可以覆盖所有ScrollBar的缩略图最小宽度。使用此设置有两个重要原因。
1)这不会调整ScrollBarRepeatButton的大小。(为什么样式覆盖Track
2)这将 * 只 * 调整ScrollBar中使用的Track控件的缩略图大小。(为什么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>
yjghlzjz

yjghlzjz4#

如果您正在寻找如何设置滚动条滑块的最小高度:
来自伊恩(真实的的MVP)here

scrollBar1.Track.ViewportSize = double.NaN;  
scrollBar1.Track.Thumb.Height = Math.Max(minThumbHeight, DataScrollBar.Track.Thumb.ActualHeight);

或者您知道,添加100多行xaml代码会导致omgDATABASE!!1!

0tdrvxhp

0tdrvxhp5#

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;
    }

相关问题