我有列表框的问题.在我的程序中,我点击一个ListBoxItem,我想更改/打开窗口并预先订购它.但问题是它首先触发事件然后改变选择.码:
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我用MessageBox.Show(LB_Playlist.SelectedIndex.ToString())尝试它时; 它正在发挥作用,选择正在改变,但信息正在显示.有没有办法改变它?
在SelectionChangedEventArgs将包含项目被取消,并被选为该项目。使用e.AddedItems来获取新选择的项目。例如
var addedItems = e.AddedItems;
if(addedItems.Count > 0)
{
var selectedItem = addedItems[0];
File_Load_List(selectedItem.ToString(), _tempList);
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您无需担心事件是在控件更新之前还是之后引发,但您确实知道事件参数包含正确的信息。
Kub*_*zyk -3
解决了。刚刚添加 Thread.Sleep(70); 在 try&catch 之前。