我在MouseOver上的列表框中的项目有不同的样式,这会产生轻微的缩放效果。这很好用,但由于ZIndex在订单中设置的项目被添加到ListBox,缩放的项目将被绘制在下一个项目的后面。我想设置它以使缩放的项目位于顶部。
我尝试过创建一个MouseOver事件处理程序,并像这样设置ZIndexProperty
private void ListItem_MouseEnter(object sender, MouseEventArgs e)
{
var grid = sender as Grid;
grid.SetValue(Canvas.ZIndexProperty, 5);
}
这不起作用,如果我在没有设置它的情况下检查ZIndex,我总是得到0所以它就像我没有看到正确的值。如何修改正确的ZIndexProperty?
答案 0 :(得分:1)
您没有包含相关的Xaml,所以我很难告诉ListItem_MouseEnter是什么事件的处理程序。如果它是ListBoxItem的MouseEnter事件的处理程序,则发送方将不是网格。
要在MouseOver上更改ListBoxItem的ZIndex,Xaml和下面的代码将起作用:
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进行验证。