Scrollbar size setting

I am trying to develop an algorithm related to calibrating a WPF thumb bar element.

The thumb element can be defined using the Scrollbar.ViewportSize property, but it, in turn, is associated with the Scrollbar.Minimum and Scrollbar.Maximum .

What I have discovered so far:

For the minimum and maximum values ​​of 0 and 10, the ViewportSize parameter:

0 - Minimum Thumb 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.

[note: these figures are taken only from my crude trial and error!]

Ideally, I would like to have an algorithm that sets the minimum and maximum values ​​for the scroll bar. I can set my thumb size to exactly% of the available track.

Can anyone help with this?

Thanks.

+6
wpf slider scrollbar
source share
2 answers

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

thumbSize = (ViewportSize / (high-low + viewportSize)) Γ— trackLength

or reinstall for viewportSize:

viewportSize = thumbSize Γ— (high-low) / (trackLength-thumbSize)

You probably already found this, but you thought that I would publish if others get here.

+11
source share

On my side, I kept the minimum finger length because the touch inputs require a minimum thumb size to optimize touch.

You can define a ScrollViewer ControlTemplate that will use the TouchScrollBar as its horizontal and vertical ScrollBar.

See the UpdateViewPort method for math.

Sorry, I do not see a use case for explicitly setting the thumb of the scroll bar 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 } 

}

+4
source share

All Articles