WPF 从ListBox .SelectedItem 中获取ListBoxItem

转自:http://geekswithblogs.net/thibbard/archive/2008/02/13/wpf---get-listboxitem-from-listbox.selecteditem.aspx

 

I had some trouble today getting the actual control that hosts data in a WPF listbox.  The magic class is ListBox.ItemContainerGenerator used like this:

MyStateObject current = this.myListBox.SelectedItem as MyStateObject;
ListBoxItem lbi = this.myListBox.ItemContainerGenerator.ContainerFromItem(current) as ListBoxItem;
lbi.Margin = new Thickness(10);
 
posted @ 2011-08-25 09:53  孤狼晖  阅读(1307)  评论(0编辑  收藏  举报