包含RichTextBoxes的C#网格视图垂直滚动

时间:2019-02-11 11:03:07

标签: c# wpf

我有一个包含大量RichTextBoxes的网格视图,并且它们每个都设置为“自动高度”,因此不需要在其中滚动。 问题是我无法在网格中滚动才能查看所有文本框。

如何设置滚动显示在网格上而不在文本框中?

这是我的C#代码:

    public MainWindow()
    {
        InitializeComponent();



        Dictionary<string, List<string>> textDic = wnXMLDic();

        foreach (KeyValuePair<string, List<string>> kv in textDic)
        {
            FlowDocument wnDoc = new FlowDocument();

            Paragraph wnParagraph = new Paragraph();

            wnParagraph.Inlines.Add(new Bold(new Run("Version "+kv.Key+":")));
            wnParagraph.Inlines.Add(Environment.NewLine);
            foreach (string val in kv.Value)
                wnParagraph.Inlines.Add(val + Environment.NewLine);

            wnDoc.Blocks.Add(wnParagraph);

            RichTextBox wnRichTextBox = new RichTextBox();
            wnRichTextBox.IsReadOnly = true;
            wnRichTextBox.Document = wnDoc;
            RowDefinition rowDef = new RowDefinition();
            rowDef.Height = GridLength.Auto;
            wnGrid.RowDefinitions.Add(rowDef);

            wnGrid.Children.Add(wnRichTextBox);
            Grid.SetRow(wnRichTextBox, wnGrid.RowDefinitions.Count - 1);
        }
    }

这是我的WPF代码:

<Window x:Class="WhatsNewWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WhatsNewWPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Name="wnGrid" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.PanningMode="VerticalOnly">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="What's New:" FontWeight="Bold" FontSize="18"/>
    </Grid>
</Window>

0 个答案:

没有答案