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

你可能感兴趣的:(listbox)