2009-02-03 40 views
0

我對MouseOver上的列表框中的項目有不同的樣式,它給出了輕微的縮放效果。這個效果很好,但由於ZIndex在訂單中設置,所以項目被添加到列表框中,縮放的項目將被繪製在下一個項目的後面。我想設置它,以便縮放的項目位於頂部。如何在MouseEnter上設置列表框項目的ZIndex?

我試圖創建一個鼠標懸停事件處理程序,並設置ZIndexProperty這樣

private void ListItem_MouseEnter(object sender, MouseEventArgs e) 
    { 
     var grid = sender as Grid;    
     grid.SetValue(Canvas.ZIndexProperty, 5); 
    } 

這是不行的,如果我檢查Z-索引沒有設置它在所有的,我總是得到0,所以它的就像我沒有看到正確的價值。我如何修改正確的ZIndexProperty?

回答

1

您不包含相關的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進行驗證。

相關問題