wpf 设置滚动条拇指大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3116287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the Scrollbar Thumb size
提问by Ash
I am attempting to work out the algorithm associated with sizing of the WPF Scrollbar thumb element.
我正在尝试计算与 WPF 滚动条拇指元素的大小相关的算法。
The thumb element can be sized using the Scrollbar.ViewportSize
property, but it in turn is related to the Scrollbar.Minimum
and Scrollbar.Maximum
values.
可以使用Scrollbar.ViewportSize
属性调整拇指元素的大小,但它又与Scrollbar.Minimum
和Scrollbar.Maximum
值相关。
What I have discovered so far is:
到目前为止我发现的是:
For a Minimum and Maximum of 0and 10, a ViewportSize of:
对于0和10的最小值和最大值,ViewportSize 为:
0 - Thumb minimum size
5 - Thumb approximately 25% of the available track
10 - Thumb approximately 50% of the available track
100 - Thumb approximately 75% of the available track
1000 - Thumb approximately 90% of the available track
10000 - Thumb fills the available track.
0 - Thumb 最小尺寸
5 - Thumb 大约 25% 的可用曲目
10 - Thumb 大约 50% 的可用曲目
100 - Thumb 大约 75% 的可用曲目
1000 - Thumb 大约 90% 的可用曲目
10000 - Thumb 填充可用轨道。
[note: these figures are only from my rough trial and error!]
[注:这些数字仅来自我粗略的试错!]
Ideally I'd like to be able to have an algorithm where given the minimum and maximum values for the Scrollbar I can set the thumb size to be exactly x% of the available track.
理想情况下,我希望能够有一个算法,其中给定滚动条的最小值和最大值,我可以将拇指大小设置为可用轨道的 x%。
Can anyone help with this?
有人能帮忙吗?
Thanks.
谢谢。
回答by Gavin S
From: http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.track(VS.90).aspx
来自:http: //msdn.microsoft.com/en-us/library/system.windows.controls.primitives.track(VS.90).aspx
thumbSize = (viewportSize/(maximum–minimum+viewportSize))×trackLength
拇指尺寸 =(视口尺寸/(最大-最小+视口尺寸))×轨道长度
or re-arranging for viewportSize:
或重新安排视口大小:
viewportSize = thumbSize×(maximum-minimum)/(trackLength-thumbSize)
视口尺寸 = 拇指尺寸×(最大-最小)/(轨迹长度-拇指尺寸)
You've prob found this already but thought I'd post in case others end up here.
你可能已经发现了这个,但我想我会发布以防其他人最终来到这里。
回答by Nadzzz
On my side, I preserved a minimum thumb length because touch inputs require a thumb of a minimum size to be touch optimized.
就我而言,我保留了最小拇指长度,因为触摸输入需要最小尺寸的拇指才能进行触摸优化。
You can define a ScrollViewer ControlTemplate that will use the TouchScrollBar as its horisontal and vertical ScrollBar.
您可以定义一个 ScrollViewer ControlTemplate,它将使用 TouchScrollBar 作为其水平和垂直滚动条。
See UpdateViewPort method for the math.
数学参见 UpdateViewPort 方法。
Sorry, I don't see the use case for explicitly setting the scrollbar thumb to cover a percentage of the track length
抱歉,我没有看到明确设置滚动条滑块以覆盖轨道长度百分比的用例
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
}
}
}
回答by sean
If you're looking for how to set a minimum height for the scrollbar thumb:
如果您正在寻找如何设置滚动条缩略图的最小高度:
From Ian (da real MVP) here:
来自 Ian(真正的 MVP)在这里:
scrollBar1.Track.ViewportSize = double.NaN;
scrollBar1.Track.Thumb.Height = Math.Max(minThumbHeight, DataScrollBar.Track.Thumb.ActualHeight);
Or you know, add 100+ lines of xaml code cause omgDATABINDING!!1!
或者您知道,添加 100 多行 xaml 代码会导致 omgDATABINDING!!1!
回答by hwu
Scrollbar thumb size for UWP:
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;
}
回答by trigger_segfault
Here's a method that will override the thumb minimum width for all ScrollBar
s. There's 2 important reasons for using this setup.
这是一个将覆盖所有ScrollBar
s的拇指最小宽度的方法。使用此设置有两个重要原因。
1) This will not resize the ScrollBar
RepeatButton
s. (Why the style overrides Track
)
1) 这不会调整ScrollBar
RepeatButton
s 的大小。(为什么样式会覆盖Track
)
2) This will onlyresize the thumbs for Track
controls that are used in ScrollBar
s. (Why the Track
style is contained in a ScrollBar
style.
2) 这只会调整sTrack
中使用的控件的拇指大小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>