gpt4 book ai didi

wpf - 如何链接滚动条和滚动查看器

转载 作者:行者123 更新时间:2023-12-04 13:10:08 28 4
gpt4 key购买 nike

我目前有两个 ScrollViewer,它们包含同一集合的备用 View 。我通过处理 ScrollChanged 事件并使用 ScrollToVerticalOffset 将两个滚动查看器的滚动绑定(bind)在一起。

出于演示的原因,我将两个 ScrollViewer 滚动条都设置为隐藏,并希望从单独的 ScrollBar 控制它们。

这似乎并不简单。我记得几个月前看过一篇关于它的博客,但我再也找不到了。

任何人都可以为我指出一些有用资源的方向,或者让我朝着正确的方向插入它如何实现。

提前致谢。

最佳答案

太好了,正是我需要的。我扩展了一点,以便也可以使用依赖属性从 xaml 设置滚动查看器。在xml中:

<local:BindableScrollBar BoundScrollViewer ="{Binding ElementName=ScrollViewer}"  Orientation="Vertical" />

代码:
    /// <summary>
/// An extended scrollbar that can be bound to an external scrollviewer.
/// </summary>
public class BindableScrollBar : ScrollBar
{
public ScrollViewer BoundScrollViewer
{
get { return (ScrollViewer)GetValue(BoundScrollViewerProperty); }
set { SetValue(BoundScrollViewerProperty, value); }
}

// Using a DependencyProperty as the backing store for BoundScrollViewer. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BoundScrollViewerProperty =
DependencyProperty.Register("BoundScrollViewer", typeof(ScrollViewer), typeof(BindableScrollBar), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnBoundScrollViewerPropertyChanged)));

private static void OnBoundScrollViewerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
BindableScrollBar sender = d as BindableScrollBar;
if (sender != null && e.NewValue != null)
{
sender.UpdateBindings();
}
}



/// <summary>
/// Initializes a new instance of the <see cref="BindableScrollBar"/> class.
/// </summary>
/// <param name="scrollViewer">The scroll viewer.</param>
/// <param name="o">The o.</param>
public BindableScrollBar(ScrollViewer scrollViewer, Orientation o)
: base()
{
this.Orientation = o;
BoundScrollViewer = scrollViewer;
}

/// <summary>
/// Initializes a new instance of the <see cref="BindableScrollBar"/> class.
/// </summary>
public BindableScrollBar() : base() { }

private void UpdateBindings()
{
AddHandler(ScrollBar.ScrollEvent, new ScrollEventHandler(OnScroll));
BoundScrollViewer.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(BoundScrollChanged));
Minimum = 0;
if (Orientation == Orientation.Horizontal)
{
SetBinding(ScrollBar.MaximumProperty, (new Binding("ScrollableWidth") { Source = BoundScrollViewer, Mode = BindingMode.OneWay }));
SetBinding(ScrollBar.ViewportSizeProperty, (new Binding("ViewportWidth") { Source = BoundScrollViewer, Mode = BindingMode.OneWay }));
}
else
{
this.SetBinding(ScrollBar.MaximumProperty, (new Binding("ScrollableHeight") { Source = BoundScrollViewer, Mode = BindingMode.OneWay }));
this.SetBinding(ScrollBar.ViewportSizeProperty, (new Binding("ViewportHeight") { Source = BoundScrollViewer, Mode = BindingMode.OneWay }));
}
LargeChange = 242;
SmallChange = 16;
}

private void BoundScrollChanged(object sender, ScrollChangedEventArgs e)
{
switch (this.Orientation)
{
case Orientation.Horizontal:
this.Value = e.HorizontalOffset;
break;
case Orientation.Vertical:
this.Value = e.VerticalOffset;
break;
default:
break;
}
}

private void OnScroll(object sender, ScrollEventArgs e)
{
switch (this.Orientation)
{
case Orientation.Horizontal:
this.BoundScrollViewer.ScrollToHorizontalOffset(e.NewValue);
break;
case Orientation.Vertical:
this.BoundScrollViewer.ScrollToVerticalOffset(e.NewValue);
break;
default:
break;
}
}
}

关于wpf - 如何链接滚动条和滚动查看器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4330497/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com