【问题标题】:ScrollViewer Not Scrolling To Bottom Using AvaloneditScrollViewer 不使用 Avalonedit 滚动到底部
【发布时间】:2014-12-28 00:10:07
【问题描述】:

我创建了一个使用 avalonedit 控件的用户控件。用户控件的 XAML 是:

<UserControl x:Class="CodeNote.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"         
             xmlns:editing="clr-namespace:ICSharpCode.AvalonEdit.Editing">

    <UserControl.Resources>
        <ControlTemplate x:Key="TextBoxBaseControlTemplate1" TargetType="{x:Type TextBoxBase}">
            <Border Background="{TemplateBinding Background}" 
                x:Name="Bd" BorderBrush="#D1D1E8"
                BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10, 10, 0, 0">
                <ScrollViewer x:Name="PART_ContentHost"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
                <Trigger Property="Width" Value="Auto">
                    <Setter Property="MinWidth" Value="100"/>
                </Trigger>
                <Trigger Property="Height" Value="Auto">
                    <Setter Property="MinHeight" Value="20"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>


    </UserControl.Resources>



    <Grid>
        <StackPanel Margin="0,0,0,10">
            <TextBox x:Name="txtTitle" VerticalContentAlignment="Center" Template="{StaticResource TextBoxBaseControlTemplate1}" FontWeight="Bold" Margin="5,5,5,0" Padding="5, 3, 5, 2" FontFamily="Arial" FontSize="12" BorderThickness="1,1,1,0" Background="#FFF0F0F0"></TextBox>

            <Border BorderThickness="1" CornerRadius="0,0,10,10" BorderBrush="#D1D1E8" Background="#FFF7F7F9" Margin="5,0,5,0" Padding="5,5,5,5">
                <avalonEdit:TextEditor
                    xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
                    Name="textEditor"
                    FontFamily="Courier New"
                    SyntaxHighlighting="Java"
                    Background="#FFF7F7F9"
                    ShowLineNumbers="True"
                    VerticalScrollBarVisibility="Hidden"
                    HorizontalScrollBarVisibility ="Hidden" 
                    WordWrap="True"                    
                    FontSize="12pt"/>
            </Border>


        </StackPanel>
    </Grid>

</UserControl>

主窗口在 Grid 中的 ScrollViewer 中包含以下 StackPanel

<Window x:Class="CodeNote.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"         
        xmlns:editing="clr-namespace:ICSharpCode.AvalonEdit.Editing"
        x:Name="mainWin"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ScrollViewer VerticalScrollBarVisibility="Visible" CanContentScroll="False">

            <StackPanel Grid.Row="0" Margin="10,10,10,0" VerticalAlignment="Top"  x:Name="container">



            </StackPanel>

        </ScrollViewer>

    </Grid>


</Window>

用户应该能够以编程方式将用户控件添加到主窗口,这是在主窗口后面的代码中完成的:

UserControl1 avEditor = new UserControl1(); container.Children.Add(avEditor);

我的问题是,当 avalonedit 控件的内容对于窗口而言在垂直方向上太大时,滚动查看器不会滚动到底部。插入点从可见窗口的底部消失,滚动位置保持在顶部。

我注意到,如果我添加常规文本框而不是 avalonedit 控件,滚动可以正常工作。

我该如何解决这个问题(我对 WPF 很陌生)

请注意,程序需要能够向此滚动查看器添加多个文本输入控件,例如 avalonedit 后跟一个文本框,然后是另一个文本框,然后是另一个 avalonedit。因此,我不能只使用滚动查看器的 ScrollToBottom 方法,因为正在编辑的控件可能不是滚动查看器中的最后一个控件。

我只需要插入点保持在屏幕上并相应地滚动窗口。

【问题讨论】:

    标签: c# .net wpf avalonedit


    【解决方案1】:

    似乎Caret.BringCaretToView() 只滚动 AvalonEdit 本身,它不会向可视树发送BringIntoView 请求。

    我认为您需要更改 AvalonEdit 源代码来解决此问题。

    【讨论】:

      【解决方案2】:

      我不是专家,但下面是我使用 AvalonEdit 的伪尾巴。 _scroll 只是一个复选框,用户可以取消选中以停止滚动。

      private void PullAndLoadData()
      
          {
              Task.Delay(1000);
              TailFile = TailFile + _logStream.Read();
              Dispatcher.Invoke(() => { textEditor.Text = TailFile; });
          
              if (_scroll) Dispatcher.Invoke(() => { textEditor.ScrollToEnd(); });
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-12
        • 2013-02-14
        • 2014-12-12
        • 2018-10-22
        相关资源
        最近更新 更多