如何链接滚动条和scrollviewer [英] How to link scrollbar and scrollviewer
问题描述
我目前有两个ScrollViewer,其中包含同一集合的备用视图.我通过处理ScrollChanged事件并使用ScrollToVerticalOffset将两个scrollviewer的滚动绑定在一起.
I currently have two ScrollViewer's containing alternate views of the same collection. I have bound the scrolling of the two scrollviewers together by handling the ScrollChanged event and using ScrollToVerticalOffset.
出于演示的原因,我将两个ScrollViewer滚动条都设置为隐藏,并希望通过单独的ScrollBar对其进行控制.
For presentation reasons I have set both ScrollViewer scrollbars to hidden and want to control them both from a seperate ScrollBar.
这似乎并不简单.我记得几个月前看到一个有关它的博客,但我找不到它.
This seems to not be straightforward. I recall seeing a blog about it a few months ago but I can't find it again.
任何人都可以向我指出一些有用的资源,或者向我推销如何实现它的正确方向.
Can anyone point me in the direction of some useful resources or give me a shove in the right direction to how it might be achieved.
谢谢.
推荐答案
好,解决了这个问题.实际上非常简单.
Ok, solved this. Was actually quite straightforward.
从此找到 Wpf绑定到函数,这应该可以帮助其他感兴趣的人.它的VB,但应该足够清楚.
Have since found Wpf binding to a function, which should help anyone else interested. Its VB but should be clear enough.
欢呼
更进一步:我将ScrollBar子类化,并传入了我想绑定的ScrollViewer中. 似乎可以正常工作.
Further to above: I subclassed ScrollBar and passed in the ScrollViewer I wanted to bind. Seems to work ok.
public class ScrollViewerBoundScrollBar : ScrollBar
{
private ScrollViewer _scrollViewer;
public ScrollViewer BoundScrollViewer { get { return _scrollViewer; } set { _scrollViewer = value; UpdateBindings(); } }
public ScrollViewerBoundScrollBar( ScrollViewer scrollViewer, Orientation o ) : base()
{
this.Orientation = o;
BoundScrollViewer = _scrollViewer;
}
public ScrollViewerBoundScrollBar() : base()
{
}
private void UpdateBindings()
{
this.AddHandler(ScrollBar.ScrollEvent, new ScrollEventHandler(OnScroll));
_scrollViewer.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(BoundScrollChanged));
this.Minimum = 0;
if (Orientation == Orientation.Horizontal)
{
this.SetBinding(ScrollBar.MaximumProperty, (new Binding("ScrollableWidth") { Source = _scrollViewer, Mode = BindingMode.OneWay }));
this.SetBinding(ScrollBar.ViewportSizeProperty, (new Binding("ViewportWidth") { Source = _scrollViewer, Mode = BindingMode.OneWay }));
}
else
{
this.SetBinding(ScrollBar.MaximumProperty, (new Binding("ScrollableHeight") { Source = _scrollViewer, Mode = BindingMode.OneWay }));
this.SetBinding(ScrollBar.ViewportSizeProperty, (new Binding("ViewportHeight") { Source = _scrollViewer, Mode = BindingMode.OneWay }));
}
this.LargeChange = 242;
this.SmallChange = 16;
}
public 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;
}
}
public 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;
}
}
}
这篇关于如何链接滚动条和scrollviewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!