在WPF

时间:2016-12-05 20:28:06

标签: wpf

我被要求帮助正在构建应用程序的工作同事,该应用程序将用于在脱机环境中实现版本控制。我们需要能够使用单个ScrollViewer同时垂直滚动两个RichTextBox的内容,类似于Visual Studio中的比较工具的工作方式。我有一个可行的实现,但我想知道是否有更好的方法。它。我的代码如下。

XAML

<Window x:Class="WpfApplication2scrollviewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication2scrollviewer"
    mc:Ignorable="d"
    Title="MainWindow" Height="600" Width="825">
<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="40"/>
    </Grid.RowDefinitions>
    <ScrollViewer ScrollChanged="RichTextBox_ScrollChanged" ScrollViewer.VerticalScrollBarVisibility="Visible"
                  Grid.Row="1">
        <Grid>
    <Grid.ColumnDefinitions>
         <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>


        <RichTextBox Grid.Column="1" x:Name="bx1" >
                <FlowDocument>
                <Paragraph>It's the time of the year when plenty of people are counting down the days until Christmas, but if you are reading this there is a very good chance you are probably counting down the days to a different kind of holiday: the December 16th release of Rogue One: A Star Wars Story. We are all excited to spend that joyous day of nerd celebration with some of the best looking creatures to ever come out of the galaxy far, far away.</Paragraph>
                <Paragraph>It's the time of the year when plenty of people are counting down the days until Christmas, but if you are reading this there is a very good chance you are probably counting down the days to a different kind of holiday: the December 16th release of Rogue One: A Star Wars Story. We are all excited to spend that joyous day of nerd celebration with some of the best looking creatures to ever come out of the galaxy far, far away.</Paragraph>
                <Paragraph>It's the time of the year when plenty of people are counting down the days until Christmas, but if you are reading this there is a very good chance you are probably counting down the days to a different kind of holiday: the December 16th release of Rogue One: A Star Wars Story. We are all excited to spend that joyous day of nerd celebration with some of the best looking creatures to ever come out of the galaxy far, far away.</Paragraph>
                <Paragraph>It's the time of the year when plenty of people are counting down the days until Christmas, but if you are reading this there is a very good chance you are probably counting down the days to a different kind of holiday: the December 16th release of Rogue One: A Star Wars Story. We are all excited to spend that joyous day of nerd celebration with some of the best looking creatures to ever come out of the galaxy far, far away.</Paragraph>
                </FlowDocument>
            </RichTextBox>
        <RichTextBox Grid.Column="2"
                     ScrollViewer.VerticalScrollBarVisibility="Hidden"


                     x:Name="bx2">
        <FlowDocument>
            <Paragraph>It’s the time of the year when plenty of people are counting down the days until Christmas, but if you are reading this there is a very good chance you are probably counting down the days to a different kind of holiday: the December 16th release of Rogue One: A Star Wars Story. We are all excited to spend that joyous day of nerd celebration with some of the best looking creatures to ever come out of the galaxy far, far away.</Paragraph>
            <Paragraph>It’s the time of the year when plenty of people are counting down the days until Christmas, but if you are reading this there is a very good chance you are probably counting down the days to a different kind of holiday: the December 16th release of Rogue One: A Star Wars Story. We are all excited to spend that joyous day of nerd celebration with some of the best looking creatures to ever come out of the galaxy far, far away.</Paragraph>
            <Paragraph>It’s the time of the year when plenty of people are counting down the days until Christmas, but if you are reading this there is a very good chance you are probably counting down the days to a different kind of holiday: the December 16th release of Rogue One: A Star Wars Story. We are all excited to spend that joyous day of nerd celebration with some of the best looking creatures to ever come out of the galaxy far, far away.</Paragraph>

        </FlowDocument>
    </RichTextBox>
        </Grid>
    </ScrollViewer>
</Grid>

背后的代码

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

    private void RichTextBox_ScrollChanged(object sender,   ScrollChangedEventArgs e)
    {

        ScrollViewer scroll = (ScrollViewer)sender;
        bx1.ScrollToVerticalOffset(scroll.VerticalOffset);
        bx2.ScrollToVerticalOffset(scroll.VerticalOffset);

    }
}

0 个答案:

没有答案