【发布时间】:2020-12-31 22:51:30
【问题描述】:
我正在尝试使用 WPF 构建 Revit 插件,并且正在尝试向窗口动态添加控件。但是,这些控件没有显示在窗口中,也没有错误。任何帮助,将不胜感激。谢谢。
Xaml
<ScrollViewer Margin="0,190,-0.4,-1">
<StackPanel Name="TaskList" Height="auto" Width="auto">
</StackPanel>
</ScrollViewer>
c#
for (var i = 0; i < dt.Rows.Count; i++)
{
Canvas canvas = new Canvas();
canvas.Height = 100;
canvas.Width = 300;
canvas.Background = new SolidColorBrush(Colors.Black);
canvas.Margin = new Thickness(20);
System.Windows.Controls.TextBox tb = new System.Windows.Controls.TextBox();
tb.Background = new SolidColorBrush(Colors.Black);
tb.Foreground = new SolidColorBrush(Colors.White);
tb.Width = 300;
tb.FontSize = 30;
tb.Height = 100;
tb.TextWrapping = TextWrapping.Wrap;
tb.MaxLength = 40;
tb.Text = dt.Rows[i][2].ToString();
canvas.Children.Add(tb);
TaskList.Children.Add(canvas);
TaskList.UpdateLayout();
}
编辑 我使用页面标签作为基础而不是窗口。也许这会改变我应该如何解决这个问题?
【问题讨论】:
-
这里为其他人提出的所有答案在我看来都是很好的解决方案,但很奇怪你还没有得到任何显示结果。您能否尝试 1) 删除任何
ScrollViewer保证金。 2) 调试您的for循环,以确保您在其中按预期循环。