111,109
社区成员




class MyListBox : ListBox
{
public MyListBox()
{
this.AddHandler(ListBoxItem.MouseEnterEvent, new RoutedEventHandler(OnMouseEnterItem));
}
void OnMouseEnterItem(object sender, RoutedEventArgs e)
{
//但在此处无论是sender还是e.OriginalSource类型都是MyListBox
}
}
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<StackPanel MouseEnter="StackPanel_MouseEnter">
<TextBlock Width="20" Background="Red" Text="{Binding}"></TextBlock>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
后台:
StackPanel panel = e.OriginalSource as StackPanel;
ListBoxItem parent = CommonHelper.GetParentObject<ListBoxItem>(panel) as ListBoxItem;
int index = list.ItemContainerGenerator.IndexFromContainer(parent); // 索引
GetParentObject方法,参见:
http://blog.csdn.net/duanzi_peng/article/details/17094639