当MainWindow调整大小时,使ScrollViewer调整大小

时间:2014-05-21 16:11:56

标签: c# wpf scrollviewer

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

它目前有一个ScrollViewer,当WPF MainWindow调整大小时不会调整大小。如何使ScrollViewer大小服从MainWindow?

    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"/>

1 个答案:

答案 0 :(得分:2)

简单地让ScrollViewer没有HeightWidthMargin属性:

<Window>
  <ScrollViewer/>
</Window>