如何在ScrollViewer中嵌套标签元素

本文关键字:嵌套 标签 元素 ScrollViewer | 更新日期: 2023-09-27 18:16:43

我想完成这样的事情:

<ScrollViewer Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="boxAnswer" VerticalAlignment="Top" Width="294">
   <Label Content="Label" Height="28" Name="label1" VerticalAlignment="top" />
</ScrollViewer>

除非我想以编程方式放置。当我在boxAnswer上调用。isancestors of时,它返回true,但我不能在标签元素上设置。is后裔of,尽管我可以通过编程创建一个。

如何在ScrollViewer中嵌套标签元素

boxAnswer.Content = new Label()
                            {
                                 Content = "Label",
                                 Height = 28,
                                 Name = "label1",
                                 VerticalAlignment = VerticalAlignment.Top
                             };

This -

ScrollViewer scrollViewer = new ScrollViewer();
        StackPanel stackPanel = new StackPanel();
        stackPanel.Orientation = Orientation.Vertical;
        scrollViewer.Content = stackPanel;
        stackPanel.Children.Add(new TextBlock(){
            Text = "some text"
    });
        stackPanel.Children.Add(new TextBlock(){
            Text = "some text2"
    });

是一种方法。虽然我认为也许你应该看看ListBox或ListView,因为它们可能是比不断增长的stackpanel更合适的解决方案。甚至可能只是一个多行文本块