gpt4 book ai didi

c# - 以编程方式添加和编辑控件

转载 作者:太空宇宙 更新时间:2023-11-03 13:38:44 26 4
gpt4 key购买 nike

我以编程方式创建一个 WPF TabItem 并将其添加到我的 TabControl。

   var tabItem = new TabItem { Header = "foo" };

现在我想做类似的事情

   var txt1 = new TextBlock { Text = "foo" };
var txt2 = new TextBlock { Text = "bar" };

var tabItem = new TabItem { Header = txt1 + txt2 }; // cannot apply operator + to TextBlock and TextBlock
// Other Idea:
// var tabItem = new TabItem { Header = new TextBlock { Text = "foo" }.Text + new TextBlock { Name = "txt2", Text = "bar" }};
// Maybe I could edit my TextBlock via it's name?
...
txt2.Text = "editedBar"; // have to update the header of tabItem.

这有可能吗?我知道在 XAML 中这不是问题。但是现有的架构迫使我尝试这种方式。

最佳答案

我会做这样的事情:

StackPanel panel = new StackPanel();
panel.Children.Add(txt1);
panel.Children.Add(txt2);

var tabItem = new TabItem { Header = panel };

关于c# - 以编程方式添加和编辑控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17867360/

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