ScrollViewer不使用Avalonedit滚动到底部

时间:2014-12-28 00:10:07

标签: c# .net wpf avalonedit

我创建了一个使用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>

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

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

我的问题是当avalonedit控件的内容对于窗口变得太垂直时,scrollviewer不会滚动到底部。插入点只是从可见窗口的底部消失,滚动位置保持在顶部。

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

我如何纠正这个问题(我是WPF的新手)

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

我只需要将插入点保留在屏幕上,然后相应地滚动窗口。

1 个答案:

答案 0 :(得分:0)

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

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