同步列表框滚动和文本框/文本块控件WPF [英] Synchronize Scroll of Listbox and Textbox/TextBlock control WPF

查看:68
本文介绍了同步列表框滚动和文本框/文本块控件WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个WPF Usercontrol,其中有三个控件



-ListBox



-TextBox或Textblock



-ScrollBar



我希望同步所有这些之间的滚动。< br $>




I am trying to create a WPF Usercontrol where there are three controls

-ListBox

-TextBox or Textblock

-ScrollBar

I wish to synchronize the scrolling between all of them.


<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100"></ColumnDefinition>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="20*" />
    </Grid.ColumnDefinitions>

    <ListBox Grid.Column="0" HorizontalAlignment="Left" Name="lstNumber" Width="100" FontFamily="Courier New" FontSize="12" Background="Aqua" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.ScrollChanged="RichTextBox_ScrollChanged"/>
    <TextBox Grid.Column="1" HorizontalAlignment="Stretch" Name="txtData" Width="Auto" FontFamily="Courier New" FontSize="12" Margin="101,0,0,0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.ScrollChanged="RichTextBox_ScrollChanged"/>
    <ScrollBar Grid.Column="3" HorizontalAlignment="Right" Scroll="ScrollBar_Scroll"/>
</Grid>



< br $>










相关C#代码对于活动。












Related C# Code for the event.


private void RichTextBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        if (sender == txtData)
        {
            //what to do now?
        }
        if (sender == lstNumber)
        {
            //what to do now?
        }
    }











如何实现此功能?

推荐答案

我试图像代码编辑器一样显示文本文件。哪里必须有3件东西



- 行号

- 当我在整个加载的文件中选择一个文本时,突出显示类似的文字。

- 滚动时,单独的滚动条必须可用

- 向上和向下,Pg Up&Pg Down,键盘上的Home&End键应该用于滚动。
I am trying to show a text file like a code editor. Where 3 things must be available

- Line number
- Highlight similar text, when I select a text in the whole loaded file.
- for scrolling, separate scrollbar must be available
- Up & down, Pg Up & Pg Down, Home & End Key from keyboard should function for scrolling.


你可以使用User32滚动





Const WM_USER As Integer =& H400

Const EM_GETSCROLLPOS As Integer = WM_USER + 221

Const EM_SETSCROLLPOS As Integer = WM_USER + 222



声明函数SendMessage Libuser32 .dllAliasSendMessageW(ByVal hWnd As IntPtr,ByVal msg As Integer,ByVal wParam As Integer,ByRef lParam As Point)As Integer



Private Sub RichTextBox2_VScroll( ByVal sender As System.Object,ByVal e As System.EventArgs)处理RichTextBox2.VScroll



Dim pt As Point



SendMessage(RichTextBox2.Handle,EM_GETSCROLLPOS,0,pt)



SendMessage(RichTextBox1.Handle,EM_SETSCROLLPOS,0,pt)



End Sub
You can use the scroll using User32


Const WM_USER As Integer = &H400
Const EM_GETSCROLLPOS As Integer = WM_USER + 221
Const EM_SETSCROLLPOS As Integer = WM_USER + 222

Declare Function SendMessage Lib "user32.dll" Alias "SendMessageW" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByRef lParam As Point) As Integer

Private Sub RichTextBox2_VScroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox2.VScroll

Dim pt As Point

SendMessage(RichTextBox2.Handle, EM_GETSCROLLPOS, 0, pt)

SendMessage(RichTextBox1.Handle, EM_SETSCROLLPOS, 0, pt)

End Sub


这篇关于同步列表框滚动和文本框/文本块控件WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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