使ScrollViewer在主窗口调整大小时调整大小

本文关键字:调整 小时 窗口 ScrollViewer | 更新日期: 2023-09-27 18:04:22

这是一个为Windows实现只读Linux文本文件(一个字节换行序列)查看器的尝试。

它现在有一个滚动查看器,当WPF主窗口被调整大小时,它不会调整大小。如何使ScrollViewer的大小服从于主窗口?

    private void ScrollViewer1GotFocus(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog dlg = 
                new Microsoft.Win32.OpenFileDialog();
        dlg.FileName = "Document";
        dlg.DefaultExt = ".txt"; 
        dlg.Filter = null;
        Nullable<bool> result = dlg.ShowDialog();
        if (result == true)
        {
            string filename = dlg.FileName;
            StreamReader streamReader = new StreamReader(filename,
                                          System.Text.Encoding.ASCII);
            string text = streamReader.ReadToEnd();
            FlowDocument flowDocument = new FlowDocument();
            flowDocument.TextAlignment = TextAlignment.Left;
            flowDocument.FontFamily = new FontFamily("Lucida Console");
            Paragraph paragraph = new Paragraph();
            paragraph.Inlines.Add(text);
            flowDocument.Blocks.Add(paragraph);
            FlowDocumentScrollViewer fdsv = new FlowDocumentScrollViewer();
            fdsv.Document = flowDocument;
            ScrollViewer1.Content = fdsv;
        }
    }
    private void MainWindow1SizeChanged(object sender, SizeChangedEventArgs e)
    {
    }
XAML:

<Window x:Name="MainWindow1" x:Class="ReadOnlyViewLinux1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ReadOnlyView" Height="256" Width="512" SizeChanged="MainWindow1SizeChanged">
<ScrollViewer x:Name="ScrollViewer1" HorizontalAlignment="Left" Height="236" Margin="10,10,0,-21" VerticalAlignment="Top" Width="492" GotFocus="ScrollViewer1GotFocus"/>

使ScrollViewer在主窗口调整大小时调整大小

让ScrollViewer没有Height, WidthMargin属性:

<Window>
  <ScrollViewer/>
</Window>