gpt4 book ai didi

c# - 如何在wpf中动态删除文本 block

转载 作者:行者123 更新时间:2023-11-30 22:26:48 25 4
gpt4 key购买 nike

public partial class Window2 : Window
{
int margin = 200;
public Window2()
{
this.InitializeComponent();

for (int i = 1; i <= 5; i++)
{
TextBlock DynamicLine = new TextBlock();
DynamicLine.Name = "lbl_DynamicLine" + i;
DynamicLine.Width = 600;
DynamicLine.Height = 20;
DynamicLine.Text =i+"Dynamic TextBlock";
DynamicLine.Margin = new Thickness(50, margin, 0, 0);
margin = margin + 20;

LayoutRoot.Children.Add(DynamicLine);

}
}
}

我尝试像下面那样动态删除文本 block 。

LayoutRoot.Children.Remove(DynamicLine);

但我只能使用上面的代码行删除最后创建的文本 block 。现在我想动态删除所有文本 block 。我该怎么做。

最佳答案

试试这段代码

public partial class Window2 : Window
{
int margin = 200;
TextBlock DynamicLine;
public Window2()
{
this.InitializeComponent();

for (int i = 1; i <= 5; i++)
{
DynamicLine = new TextBlock();
DynamicLine.Name = "lbl_DynamicLine" + i;
RegisterName(DynamicLine.Name, DynamicLine);
DynamicLine.Width = 600;
DynamicLine.Height = 20;
DynamicLine.Text =i+"Dynamic TextBlock";
DynamicLine.Margin = new Thickness(50, margin, 0, 0);
margin = margin + 20;

LayoutRoot.Children.Add(DynamicLine);

}

for (int i = 1; i <= 5; i++)
{
DynamicLine = (TextBlock)this.FindName("lbl_DynamicLine" + i);
LayoutRoot.Children.Remove(DynamicLine);
}

}
}

关于c# - 如何在wpf中动态删除文本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11577618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com