0

使用 SL5 和 Silverlight 工具包

我在 ListBox 上单击鼠标右键,效果很好:

private void Person_Click(object sender, RoutedEventArgs e)
        {
            Account account = ((MenuItem)sender).DataContext as Account;
            UpdateText.Text = string.Format("Person selected on account: <{0}>", account.Username);
        }

如果我选择了多个 ListBox 项目(SelectionMode="Extended"),我只能引用 Person_Click 中的 1 个项目

问题:如何从上下文菜单中引用多个项目?

回答(给未来的人!)

 private void Person_Click(object sender, RoutedEventArgs e)
        {
            Account account = ((MenuItem)sender).DataContext as Account;
            UpdateText.Text = string.Format("Person selected on account: <{0}>", account.Username);

            //if multiple items are selected on right click this is how to reference them
            List<int> selectedItemIndexes = new List<int>();
            foreach (object o in AccountItemsT32.SelectedItems)
            {
                //selectedItemIndexes.Add(listBox.Items.IndexOf(o));
                var x = o;
            }
        }
4

1 回答 1

0
private void Person_Click(object sender, RoutedEventArgs e)
        {
            Account account = ((MenuItem)sender).DataContext as Account;
            UpdateText.Text = string.Format("Person selected on account: <{0}>", account.Username);

            //if multiple items are selected on right click this is how to reference them
            List<int> selectedItemIndexes = new List<int>();
            foreach (object o in AccountItemsT32.SelectedItems)
            {
                //selectedItemIndexes.Add(listBox.Items.IndexOf(o));
                var x = o;
            }
        }
于 2012-11-02T14:20:21.480 回答