[英]Add listboxitems programmatically C#/XAML windows store apps
美好的一天! 我在可扩展/可折叠列表框中有问题。 这是我的代码:
bool add = true;
public void AddList() {
string[] win = { "Windows1", "Windows2" };
foreach (string value in win)
{
ListMain.Items.Add(value);
}
}
public void RemoveList() {
string[] win = { "Windows1", "Windows2" };
foreach (string value in win)
{
ListMain.Items.Remove(value);
}
}
private void ListMain_Tapped_1(object sender, TappedRoutedEventArgs e)
{
if (add == true)
{
this.AddList();
add = false;
}
else {
this.RemoveList();
add = true;
}
我的ListMain是ListBox,所以每次我点击它时,它都会在其中创建项目,即windows1和windows2,当我再次点击我的listmain时,它将删除windows1和window2。 我的问题是,当我点击Windows1或Windows2时,它会自动删除自身,似乎它与Listmain共享了同一点击事件,我如何更改Windows1和Windows2的点击事件。 感谢您的理解。 TYIA!
如果您想筛选出的Tapped
上的事件ListBoxItems
-添加事件处理程序,以Tapped
的ListBoxItems
并设置e.Handled = true;
因此它不会冒泡到ListBox
。 当然,在您的情况下,您无权访问ListBoxItems
因此您可能需要稍微更改代码,以便将ListBoxItems
显式添加到ListBox
而不是依靠它为您创建那些代码。
var lbi = new ListBoxItem { Content = value };
lbi.Tapped += this.OnListBoxItemTapped;
ListMain.Items.Add(lbi);
private void OnListBoxItemTapped(object sender, TappedRoutedEventArgs e)
{
e.Handled = true;
}
记住要从事件处理程序中删除RemoveList
调用中的ListBoxItems
和ubsubscribe:
lbi.Tapped -= this.OnListBoxItemTapped;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.