Windows Phone 7 - 取消选择嵌套列表框中的 ListBoxItem

发布于 2024-09-26 23:59:43 字数 1255 浏览 0 评论 0原文

我有一个带有日期的列表框。
每个 ListBoxItem(日期)都有另一个包含该日期事件的 ListBox。

当我选择一个事件时,它会突出显示(SelectedIndex/SelectedItem),并且我导航到另一个数据透视表。这很好用。

我的问题是每个列表框都有它自己的 SelectedItem。我想从每个列表框中清除 SelectedItem,但我无法让它工作!

这是我的尝试:

    //Store a reference to the latest selected ListBox
    public ListBox SelectedListBox { get; set; }

    private void SelectionChangedHandler(object sender, SelectionChangedEventArgs e)
    {
        ListBox lstBox = ((ListBox)sender);

        //This row breaks the SECOND time!!
        var episode = (Episode)lstBox.SelectedItem;   

        episodeShowName.Text = episode.Show; //Do some code
        episodeTitle.Text = episode.Name; //Do some code
        episodeNumber.Text = episode.Number; //Do some code
        episodeSummary.Text = episode.Summary; //Do some code

        resetListBox(lstBox); //Do the reset !

        pivot1.SelectedIndex = 1;
    }


    private void resetListBox(ListBox lstBox)
    {
        if (SelectedListBox != null)
            SelectedListBox.SelectedIndex = -1;

        //If I remove this line, the code doesn't break anymore
        SelectedListBox = lstBox; //Set the current ListBox as reference
    }

var Episode 第二次为空。怎么会?

I have a ListBox with dates.
Each ListBoxItem (date) have another ListBox with that date's events.

When I select an event it gets highlighted (SelectedIndex/SelectedItem) and I navigate to another Pivot. This works fine.

My problem is that every ListBox has it's own SelectedItem. I want to clear the SelectedItem from each ListBox, but I cannot get it to work!

Here's my try:

    //Store a reference to the latest selected ListBox
    public ListBox SelectedListBox { get; set; }

    private void SelectionChangedHandler(object sender, SelectionChangedEventArgs e)
    {
        ListBox lstBox = ((ListBox)sender);

        //This row breaks the SECOND time!!
        var episode = (Episode)lstBox.SelectedItem;   

        episodeShowName.Text = episode.Show; //Do some code
        episodeTitle.Text = episode.Name; //Do some code
        episodeNumber.Text = episode.Number; //Do some code
        episodeSummary.Text = episode.Summary; //Do some code

        resetListBox(lstBox); //Do the reset !

        pivot1.SelectedIndex = 1;
    }


    private void resetListBox(ListBox lstBox)
    {
        if (SelectedListBox != null)
            SelectedListBox.SelectedIndex = -1;

        //If I remove this line, the code doesn't break anymore
        SelectedListBox = lstBox; //Set the current ListBox as reference
    }

var episode is null the second time. How come?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀里藏娇 2024-10-03 23:59:43

我发现问题了!

private void resetListBox(ListBox lstBox)
{
    if (SelectedListBox != null)
        SelectedListBox.SelectedIndex = -1;

    //If I remove this line, the code doesn't break anymore
    SelectedListBox = lstBox; //Set the current ListBox as reference
}

当我将先前选定的 ListBox 的 SelectedIndex 设置为 -1 时,SelectionChangedHandler 事件再次被触发(当然)并搞砸了! :D

简单修复:

    private void SelectionChangedHandler(object sender, SelectionChangedEventArgs e)
    {
        ListBox lstBox = ((ListBox)sender);
        if (lstBox.SelectedIndex < 0)
            return;

I found the problem!

private void resetListBox(ListBox lstBox)
{
    if (SelectedListBox != null)
        SelectedListBox.SelectedIndex = -1;

    //If I remove this line, the code doesn't break anymore
    SelectedListBox = lstBox; //Set the current ListBox as reference
}

When I set the previous selected ListBox's SelectedIndex to -1, the SelectionChangedHandler event gets triggered again (of course) and screws up ! :D

Easy fix:

    private void SelectionChangedHandler(object sender, SelectionChangedEventArgs e)
    {
        ListBox lstBox = ((ListBox)sender);
        if (lstBox.SelectedIndex < 0)
            return;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文