0

我正在填充的 LisBox 有问题,当我点击一个项目时,它给了我错误

“价值不在预期范围内。”

当我在获取所选值的行上放置一个断点时,它说它的值为空,我之前在 xaml 中静态声明了列表项,它没有问题。

有什么可以帮助我的吗?

private void listbox_tapped(object sender, TappedRoutedEventArgs e)
    {
        ListBoxItem selected = (lbLetter.SelectedValue as ListBoxItem);

        int listitem = lbLetter.SelectedIndex;

        if (lbLetter.Items.Count != 0)
        {

            lbWord.Items.Add(selected);
        }

    }

   private void RandomizeListbox()
    {
        List<char> values = new List<char>();


        for (int i = 0; i<=MAXLETTERS; i++)
        {
            values.Add(RandomLetter());
        }
        lbLetter.ItemsSource = values;
    }

    public static char RandomLetter()
    {
        return alphabet[random.Next(alphabet.Length)];
    }
4

2 回答 2

1

可能会发生这种情况,因为 char 数组中有几个相同的值。尝试这个:

for (int i = 0; i <= MAXLETTERS; i++)
{
    var c = RandomLetter();
    if(!values.Contains(c))
        values.Add(c);
}
于 2012-11-10T16:21:47.447 回答
0

Without testing it, I can assure you that the following line is incorrect:

ListBoxItem selected = (lbLetter.SelectedValue as ListBoxItem);

The SelectedValue property does not return a ListBoxItem object, it returns a string. See http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.primitives.selector.selectedvalue.aspx for more information.

于 2012-11-12T12:00:59.460 回答