WPF列表框SelectionChanged事件 [英] WPF ListBox SelectionChanged event

查看:2529
本文介绍了WPF列表框SelectionChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框的一个问题。当我的节目,我单击一个ListBoxItem的,我想改变/打开窗口之前预购了。但问题是,它首先触发事件,然后它改变选择。代码:

I have a problem with listbox. When in my program I click on one ListBoxItem, I want to change/open the window and preorder it before. But the problem is that it firstly fires the event and then it changes selection. Code:

private void LB_Playlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (LB_Playlist.SelectedItem != null)
        {
            try
            {
                List<string> _tempList = new List<string>();
                File_Load_List(LB_Playlist.SelectedItem.ToString(), _tempList);
                LoadListIntoBox(_tempList);
                G_SongList.Visibility = Visibility.Visible;
                AnimationMove(G_Playlist, G_Playlist.Margin, new Thickness(-264, 0, 0, 0), AnimationDuration, true);
                AnimationMove(G_SongList, new Thickness(264, 0, 0, 0), new Thickness(0, 0, 0, 0), AnimationDuration, false);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

当我用$试了一下b $ b MessageBox.Show(LB_Playlist.SelectedIndex.ToString());
这是工作,选择正在改变,但消息被显示。有什么办法去改变它?

When I tried it with MessageBox.Show(LB_Playlist.SelectedIndex.ToString()); It was working, selection was changing but the message was showing. Is there any way to change it?

推荐答案

SelectionChangedEventArgs 将包含其中项被取消和所选择的项目。使用 e.AddedItems 来获得新选定的项目。 。例如

The SelectionChangedEventArgs will contain which item was deselected and which item was selected. Use e.AddedItems to get the newly selected items. e.g.

var addedItems = e.AddedItems;
if(addedItems.Count > 0)
{
    var selectedItem = addedItems[0];
    File_Load_List(selectedItem.ToString(), _tempList);
}



这样,您不必担心该事件是否之前提高或控制之后被更新,但你知道ARGS包含正确的信息的事件。

This way you do not need to worry about whether the event is raised before or after the control being updated, but you do know the event args contain the correct information.

这篇关于WPF列表框SelectionChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆