我在运行时将项目添加到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.
更多推荐
发布评论