WPF ScrollViewer在以编程方式填充的Grid中不起作用

问题描述 投票:0回答:1

我在xaml中有这种情况

<StackPanel Orientation="Vertical" Margin="5" Background="AliceBlue" Height="370">
    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <Grid Name="DescriptionsGrid" MinHeight="0" MaxHeight="370" ScrollViewer.CanContentScroll="True"></Grid>
    </ScrollViewer>
</StackPanel>

并且我用以下内容填充网格

DescriptionsGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
TextBlock textBlock = new TextBlock { Text = description, TextWrapping = TextWrapping.WrapWithOverflow };
Label label = new Label { Content = textBlock, Foreground = new SolidColorBrush(Colors.Blue) };
label.MouseLeftButtonDown += new MouseButtonEventHandler(LoadXML);
DescriptionsGrid.Children.Add(label);
Grid.SetRow(DescriptionsGrid.Children[DescriptionsGrid.Children.Count - 1], description_counter);
description_counter++;

我无法定义行高,因为描述可能足够长,无法将文本换行到新行。

不会出现滚动条,并且新元素将隐藏在下面。

任何想法?

wpf grid label scrollviewer stackpanel
1个回答
0
投票
按照@Clemens的建议,我重写了代码,发现错误出在XML中。
© www.soinside.com 2019 - 2024. All rights reserved.