c# - ListBox 的 SelectionChanged 事件第二次不起作用

标签 c# wpf xaml windows-phone-8 listbox

我正在尝试为我的应用程序创建一个设置列表,例如 Windows Phone 8 的“设置”页面。我正在使用 ListBox 来执行此操作。

我正在使用此代码打开选定的选项:

<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>

这在第一次对每个项目都有效,但如果我尝试连续两次选择相同的项目,则无效!

示例:如果我选择“登录”项,则会打开登录选项页面。伟大的!但是,如果我按下后退按钮并尝试再次打开“登录”项,什么也没有发生!该事件不会再次触发。

我做错了什么? ListBox 是解决我的问题的正确选择吗?

谢谢!

更新

将字段 SelectedIndex 的 ListBox 值更改为“-1”后,有效!谢谢@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 只会在所选项目实际更改时触发。听起来好像您连续点击了同一个项目两次。

关于c# - ListBox 的 SelectionChanged 事件第二次不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25320124/

相关文章:

c# - 如何在 MVC Controller 中使用 Linq 连接两个表

c# - 如何将多个处理程序添加到 XAML 中的附加事件?

c# - 当文本框为空或来自 xaml 的空格时禁用按钮

c# - 间歇性 SecureChannelFailure 错误

c# - Json post int 类型始终为 0

.net - 响应式 UI 如何使用 WhenAny 使用两个属性?

c# - 在 Windows 8 Metro 应用程序中禁用鼠标悬停焦点

c# - 如何在 Metro 应用程序中打印文本文件或文本框的内容?

c# - 在 CustomActions 类中设置项目窗口处理程序

c# - 来自 TransformedBitmap 对象的“跨越”困境