简体   繁体   中英

SelectionChanged event of a ListBox is not working in the second time

I'm trying to do a list of settings for my application, like the Settings page for the Windows Phone 8. I'm using the ListBox to do this.

I'm using this code to open the selected option:

<ListBox x:Name="lstConfigOptions"
         SelectionMode="Single" 
         SelectionChanged="lstConfigOptions_SelectionChanged">
    <ListBox.Items>
        <ListBoxItem x:Name="login" >
            <StackPanel Orientation="Horizontal" Margin="12,2,0,4">
                <StackPanel Width="311" Margin="0,-7,0,0">
                    <TextBlock Text="login" TextWrapping="NoWrap" Margin="12,0,0,0" 
                               Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                    <TextBlock Text="usuário, senha" TextWrapping="NoWrap" Margin="12,-6,0,0" 
                               Style="{StaticResource PhoneTextSubtleStyle}"/>
                </StackPanel>
            </StackPanel>
        </ListBoxItem>
        <ListBoxItem x:Name="about" >
            <StackPanel Orientation="Horizontal" Margin="12,2,0,4" >
                <StackPanel Width="311" Margin="0,-7,0,0">
                    <TextBlock Text="about" TextWrapping="NoWrap" Margin="12,0,0,0" 
                               Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                    <TextBlock Text="version, authors" TextWrapping="NoWrap" Margin="12,-6,0,0" 
                               Style="{StaticResource PhoneTextSubtleStyle}"/>
                </StackPanel>
            </StackPanel>
        </ListBoxItem>
    </ListBox.Items>
</ListBox>

This works fine in the first time for each item, but not works if I try to choose the same item two times in a row!

Example: if I choose "login" item, the login option page is opened. Great! But, if I press the back button and try to open again the "login" item, nothing happen! The event is not fired again.

What I'm doing wrong? Is ListBox the right choice for my problem?

Thanks!

Update

After change the ListBox value for the field SelectedIndex to "-1", works! Thanks @Rang

private void lstConfigOptions_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBox lBox = (sender as ListBox);
    ListBoxItem lbi = lBox.SelectedItem as ListBoxItem;
    if (lbi != null && lbi.Name == "login")
    {
        Dispatcher.BeginInvoke(() =>
        {
            NavigationService.Navigate(new Uri("/Pages/ConfigLoginPage.xaml", UriKind.Relative));
        });
    }
    lBox.SelectedIndex = -1;

}

SelectionChanged will only fire when the selected item is actually changed. It sounds like you are clicking the same item twice in a row.

well, I add my answer: If you press the back button, make sure that ListBox's selectindex = -1 .

In addition, I advice you to do it like this:

xaml:

 <ListBox x:Name="lbMenu" HorizontalAlignment="Left" Height="187" Margin="115,56,0,0" VerticalAlignment="Top" Width="209" MouseDoubleClick="ListBox_MouseDoubleClick">
            <ListBox.Items>
                <ListBoxItem Content="login" x:Name="login" >

                </ListBoxItem>
                <ListBoxItem Content="about" x:Name="about" >

                </ListBoxItem>
            </ListBox.Items>
        </ListBox>

code:

private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBoxItem item = lbMenu.SelectedItem as ListBoxItem;
            if(item != null)
            {
                switch (item.Name)
                {
                    case "login":
                        MessageBox.Show("login");
                        break;
                    case "about":
                        MessageBox.Show("about");
                        break;
                }

            }
        }

In this way, you can avoid those issues like clicking the same item twice and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM