您没有包含相关的 Xaml,因此我很难判断 ListItem_MouseEnter 是什么事件的处理程序。如果它是 ListBoxItem 的 MouseEnter 事件的处理程序,则发送者将不是 Grid。
要在 MouseOver 上更改 ListBoxItem 的 ZIndex,Xaml 和下面的代码将起作用:
页面.xaml
<UserControl x:Class="SilverlightApplication1.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<ListBox x:Name="ListBox1">
<ListBoxItem Content="Test 1" MouseEnter="ListBoxItem_MouseEnter" />
<ListBoxItem Content="Test 2" MouseEnter="ListBoxItem_MouseEnter" />
<ListBoxItem Content="Test 3" MouseEnter="ListBoxItem_MouseEnter" />
<ListBoxItem Content="Test 4" MouseEnter="ListBoxItem_MouseEnter" />
</ListBox>
</Grid>
</UserControl>
页面.xaml.cs:
using System;
using System.Windows.Controls;
using System.Windows.Input;
namespace SilverlightApplication1
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void ListBoxItem_MouseEnter(object sender, MouseEventArgs e)
{
ListBoxItem listBoxItem = (ListBoxItem)sender;
listBoxItem.SetValue(Canvas.ZIndexProperty, 5);
}
}
}
请注意,事件处理程序针对每个 ListBoxItem 的 MouseEnter 事件,这意味着发送者是 ListBoxItem。
ListBoxItem_MouseEnter 方法将 MouseEnter 上的 Zindex 更改为 5,使用Silverlight Spy进行验证。