UWP / C#更改运行时间添加控件的值(UWP/C# change value of run time added control)

编程入门 行业动态 更新时间:2024-10-28 04:30:45
UWP / C#更改运行时间添加控件的值(UWP/C# change value of run time added control)

我在运行时将项目添加到ListView。

OnClick按钮将一个TextBlock添加到ListView。 这是代码:

private async void addPlayerButton_Click(object sender, RoutedEventArgs e) { Grid g = new Grid(); ColumnDefinition c1 = new ColumnDefinition(); ColumnDefinition c2 = new ColumnDefinition(); c1.Width = new GridLength(0, GridUnitType.Auto); c2.Width = new GridLength(0, GridUnitType.Auto); g.ColumnDefinitions.Add(c1); g.ColumnDefinitions.Add(c2); PlayersName dialog2 = new PlayersName(); var result2 = await dialog2.ShowAsync(); if(result2 == ContentDialogResult.Primary) { RowDefinition ri = new RowDefinition(); ri.Height = new GridLength(0, GridUnitType.Auto); g.RowDefinitions.Add(ri); TextBlock label = new TextBlock(); TextBlock counter = new TextBlock(); counter.Text = "0"; label.Text = dialog2.Text; g.Children.Add(label); g.Children.Add(counter); Grid.SetRow(label, 0); Grid.SetColumn(label, 0); Grid.SetRow(counter, 1); Grid.SetColumn(counter, 1); } list.Items.Add(g); }

所有这些都是应该的。

但是,我以后如何改变Text的值,让我们来说,控制命名计数器? 我必须从另一个函数做到这一点,而且,它看起来像我有多个控件命名计数器,因为每次单击一个按钮(addPlayerButton_Click), counter的实例添加。

如何跟踪在运行时添加的控件?

I'm adding items to ListView during run time.

OnClick button adds one TextBlock to ListView. Here is code:

private async void addPlayerButton_Click(object sender, RoutedEventArgs e) { Grid g = new Grid(); ColumnDefinition c1 = new ColumnDefinition(); ColumnDefinition c2 = new ColumnDefinition(); c1.Width = new GridLength(0, GridUnitType.Auto); c2.Width = new GridLength(0, GridUnitType.Auto); g.ColumnDefinitions.Add(c1); g.ColumnDefinitions.Add(c2); PlayersName dialog2 = new PlayersName(); var result2 = await dialog2.ShowAsync(); if(result2 == ContentDialogResult.Primary) { RowDefinition ri = new RowDefinition(); ri.Height = new GridLength(0, GridUnitType.Auto); g.RowDefinitions.Add(ri); TextBlock label = new TextBlock(); TextBlock counter = new TextBlock(); counter.Text = "0"; label.Text = dialog2.Text; g.Children.Add(label); g.Children.Add(counter); Grid.SetRow(label, 0); Grid.SetColumn(label, 0); Grid.SetRow(counter, 1); Grid.SetColumn(counter, 1); } list.Items.Add(g); }

And all of that works as it should.

But, how can I later change value of Text for, let's say, control named counter? I have to do that from another function, and also, it looks like I have more than one control named counter, because each time a button is clicked (addPlayerButton_Click), an instance of counter is added.

How do I keep track of controls added at runtime?

最满意答案

您有多个控件的引用局部变量名称是counter 。 请注意,这并不意味着您的counter名称上有多个控件。 一种选择是给每个人一个名字(每次不同),就像

counter.Name = "something"

或者把它们放在一个共同的父母之下,就像一个小组。 你也可以创建一个类级别的变量(列表,字典或类似的)。 每次点击一个按钮,添加您的本地参考。

You have more than one control for which the referring local variable name is counter. Note that it doesn't mean you have multiple controls that go by the Name of counter. One option is to give each of them a name (each time different), like

counter.Name = "something"

Or put them under a common parent, like a Panel. You can also make a class level variable (list, dictionary or similar). Each time a button is clicked, add your local reference to it.

更多推荐

本文发布于:2023-08-01 22:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   时间   change   UWP   added

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!