开发者

Listbox Selected Item in Windows Phone 7

I'm programming an app for Windows Phone 7, I made an home page with a listbox that contains all my items that are the names of the other pages. So when O tap an item I should navigate to the page, but when I tap the phone gives me an error.

this is my code:

private void NavigateToPages_Click(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem == null) {}
        else
        {
            string uri = listBox1.SelectedItem.ToString();

            switch (uri)
            {
                case "Quadratic Eq.":
                    Navigate("/Pages/EQ.xaml");
                    break;
                case "Average":
                    Navigate("/Pages/Average.xaml");
                    break;
                case "Pythagoras":
                    Navigate("/Pages/pythagoras.xaml");
                    break;
                case "Trigon开发者_C百科ometry":
                    Navigate("/Pages/Trigon.xaml");
                    break;
                case "Percentage":
                    Navigate("/Pages/Percentoff.xaml");
                    break;
                case "Prime Number":
                    Navigate("/Pages/prime.xaml");
                    break;
                case "Factorize":
                    Navigate("/Pages/Factorize.xaml");
                    break;
                case "GCD & LCD":
                    Navigate("/Pages/GG.xaml");
                    break;
                default:
                    MessageBox.Show("Select a function!");
                    break;
            }
        }

It gives me Select a function, but I've selected an item, it's the same things in my app. I wrote this code because there aren't rights events listbox item


You should be using the SelectionChanged event of ListBox rather than the method you're using.

In xaml:

 <ListBox SelectionChanged="ListBoxSelectionChanged" >

        </ListBox>

in your xaml.cs file:

  private void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ListBox lb = ((ListBox) sender);
                if (lb.SelectedIndex == -1)
                    return;

 string uri = lb.SelectedItem.ToString();

            switch (uri)
            {
                case "Quadratic Eq.":
                    Navigate("/Pages/EQ.xaml");
                    break;
                case "Average":
                    Navigate("/Pages/Average.xaml");
                    break;
                case "Pythagoras":
                    Navigate("/Pages/pythagoras.xaml");
                    break;
                case "Trigonometry":
                    Navigate("/Pages/Trigon.xaml");
                    break;
                case "Percentage":
                    Navigate("/Pages/Percentoff.xaml");
                    break;
                case "Prime Number":
                    Navigate("/Pages/prime.xaml");
                    break;
                case "Factorize":
                    Navigate("/Pages/Factorize.xaml");
                    break;
                case "GCD & LCD":
                    Navigate("/Pages/GG.xaml");
                    break;
                default:
                    MessageBox.Show("Select a function!");
                    break;
            }


                lb.SelectedIndex = -1;
            }


You should use an Hyperlink control so that the target page is already in the list item. Also you can bind that to a dynamic data source.

For example:

                           <HyperlinkButton NavigateUri="/Factorize.xaml"
                                         Style="{StaticResource HyperlinkEmptyStyle}"
                                         DataContext="{Binding}">
                            <localControls:HeaderedContentControl Style="{StaticResource MultilineHyperlinkStyle}"
                                                                  Header="{Binding Resources.menu_01}"
                                                                  Content="{Binding Resources.menu_02}"/>
                        </HyperlinkButton>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜