我創建了一個使用avalonedit控件的用戶控件。用戶控件的XAML是:ScrollViewer不滾動到底部使用Avalonedit
<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>
主窗口包含一個網格
<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>
的用戶應該能夠到該用戶控件編程方式添加到主窗口內的ScrollViewer內的下列StackPanel中,這是在主窗口代碼完成後面:
UserControl1 avEditor = new UserControl1(); container.Children.Add(avEditor);
我的問題是,滾動視圖當avalonedit控件的內容對窗口來說太大時,er不會滾動到底部。插入點從可見窗口的底部消失,滾動位置停留在頂部。
我注意到滾動工作正常,如果我添加一個常規文本框而不是avalonedit控件。
我怎樣才能改變這種(我很新的WPF)
請注意,該方案需要能夠將多個文本輸入控件添加到此滾動瀏覽器,如avalonedit後跟一個文本框,然後另一個文本框跟隨另一個avalonedit。因此,我不能只使用scrollviewer的ScrollToBottom方法,因爲正在編輯的控件可能不是scrollviewer中的最後一個控件。
我只需要插入點保持屏幕和窗口相應地滾動。