您不包含相關的Xaml,因此我很難分辨ListItem_MouseEnter是什麼事件處理程序。如果它是ListBoxItem的MouseEnter事件的處理程序,則發件人不會是網格。
在鼠標懸停在XAML中改變zIndex的一個ListBoxItem中的以下代碼將工作:
Page.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>
Page.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進行驗證。