两个ScrollViewers同步滚动,无论何时在wpf中滚动任何一个 [英] Synchronized scrolling of two ScrollViewers whenever any of one is scrolled in wpf

查看:88
本文介绍了两个ScrollViewers同步滚动,无论何时在wpf中滚动任何一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一个帖子:

绑定两个VerticalScrollBars一个到另一个 [ ^ ]

它几乎帮助实现了目标,但仍然缺少一些东西。这是左右或上下移动滚动条给出了我的两个滚动查看器中滚动的预期行为,但是当我们尝试在滚动查看器中滚动使用/单击这些滚动条末端的箭头按钮时,只滚动一个滚动查看器预期的行为。



那么我们需要添加/编辑什么来解决这个问题?

I have gone through a thread:
binding two VerticalScrollBars one to another[^]
it has almost helped to achieve the goal but still there is something missing. It is that moving the scrollbars left-right or up-down gives expected behavior of scrolling in both of my scrollviewers but when we try to scroll using/clicking arrow buttons at the ends of these scrollbars in scrollviewers only one scrollviewer is scrolled which is not the expected behavior.

So what else we need to add/edit to solve this?

推荐答案

这应该可以解决问题:



this should do the trick:

<Window x:Class="BoundScrollBarws.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="100">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ScrollViewer
            Name="_viewer1"
            ScrollChanged="ScrollViewer1_ScrollChanged"
            HorizontalScrollBarVisibility="Visible"
            Grid.Column="0">
            <StackPanel>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
                <TextBlock>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA</TextBlock>
            </StackPanel>
        </ScrollViewer>

        <ScrollViewer
            Name="_viewer2"
            ScrollChanged="ScrollViewer2_ScrollChanged"
            HorizontalScrollBarVisibility="Visible"
            Grid.Column="1">
            <StackPanel>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
                <TextBlock>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB</TextBlock>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Window>







public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
    }

    private void ScrollViewer1_ScrollChanged(object sender, ScrollChangedEventArgs e) {
        _viewer2.ScrollToHorizontalOffset(e.HorizontalOffset);
        _viewer2.ScrollToVerticalOffset(e.VerticalOffset);
    }

    private void ScrollViewer2_ScrollChanged(object sender, ScrollChangedEventArgs e) {
        _viewer1.ScrollToHorizontalOffset(e.HorizontalOffset);
        _viewer1.ScrollToVerticalOffset(e.VerticalOffset);
    }
}





btw:没有通过绑定找到一个快速的方法



btw: didn`t found a quick way doing this via binding




您应该使用Scroll事件的ValueChanged事件:



而不是



Hi,
you should use the ValueChanged event insteat of Scroll event:

Instead of

<textbox name="scrlTB1" height="100" scrollbar.scroll="Scroll" scrollviewer.verticalscrollbarvisibility="Visible" />
<textbox name="scrlTB2" height="100" scrollbar.scroll="Scroll" scrollviewer.verticalscrollbarvisibility="Visible" />

private void Scroll(object sender, ScrollEventArgs e)
{
    if (sender == scrlTB1)
    {
        scrlTB2.ScrollToVerticalOffset(e.NewValue);
    }
    else
    {
        scrlTB1.ScrollToVerticalOffset(e.NewValue);
    }
}





你应该试试这个:







you should try this:


<textbox x:name="txt_1" height="122" textwrapping="Wrap" text="TextBox" scrollviewer.verticalscrollbarvisibility="Visible" scrollbar.valuechanged="ScrollBar_Scroll" scrollviewer.cancontentscroll="True" xmlns:x="#unknown" />

<textbox x:name="txt_2" height="122" margin="346,169,0,0" textwrapping="Wrap" text="TextBox" width="139" scrollviewer.verticalscrollbarvisibility="Visible" scrollbar.valuechanged="ScrollBar_Scroll" scrollviewer.cancontentscroll="True" xmlns:x="#unknown" />

private void ScrollBar_Scroll(object sender, RoutedPropertyChangedEventArgs<double> e)
{            
    if (sender == txt_1)
    {
        txt_2.ScrollToVerticalOffset(e.NewValue);
    }
    else
    {
        txt_1.ScrollToVerticalOffset(e.NewValue);
    }
}
</double>





主要属性为:



Main properties are:

scrollviewer.verticalscrollbarvisibility="Visible" 
scrollbar.valuechanged="ScrollBar_Scroll" 
scrollviewer.cancontentscroll="True"



这将用于移动滚动条,单击滚动按钮并使用光标。



我希望这会有所帮助。



问候

Jegan


This will work on moving the scroll bar, clicking on the scroll buttons and using the cursor.

I hope this helps.

Regards
Jegan


这篇关于两个ScrollViewers同步滚动,无论何时在wpf中滚动任何一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆