如何在ScrollViewer中嵌套标签元素?

时间:2011-12-05 06:13:19

标签: c# wpf visual-studio-2010

我正在努力完成这样的事情:

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

除了我想以编程方式放置。当我调用.IsAncestorOf on boxAnswer它返回true但我无法在标签元素上设置.IsDescendentOf,即使我可以以编程方式创建一个。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

这 -

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,因为它们可能比一个不断增长的堆栈面板更合适。甚至可能只是一个多行文本块。

相关问题