Silverlight设置UserControl的ZIndex

时间:2011-02-02 19:53:31

标签: c# silverlight

如何设置UserControl的ZIndex? 我试过了

Canvas.SetZIndex((Tile)sender, 99);
((Tile)sender).SetValue(Canvas.ZIndexProperty, 99);

Tile是我的UserControl的名称 我在UserControl中有一个Rectangle,它实际上出现在屏幕上。 我也试过

Canvas.SetZIndex(((Tile)sender).rect, 99);
((Tile)sender).rect.SetValue(Canvas.ZIndexProperty, 99);

编辑:

以下是我的UserControl的XAML

<UserControl x:Class="Carcassonne.Tile"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="76" d:DesignWidth="76">

    <Canvas x:Name="LayoutRoot" Background="Transparent">
        <Rectangle x:Name="rect" 
                   Width="76" Height="76"
                   HorizontalAlignment="Left">
            <Rectangle.Fill>
                <ImageBrush x:Name="tileImage">
                    <ImageBrush.RelativeTransform>
                        <RotateTransform x:Name="rotation" CenterX="0.5" CenterY="0.5" Angle="0" />
                    </ImageBrush.RelativeTransform>
                </ImageBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Canvas>
</UserControl>

1 个答案:

答案 0 :(得分:1)

以下是一些假设:

  • 您有一些外部UserControl或Page。
  • 此外部UserControl包含Canvas。
  • 此Canvas反过来有几个直接子项,它们是Carcassonne.Tile控件的实例。
  • 您已将事件处理程序附加到每个图块Left Mouse Down事件,您希望将该图块置于顶部。

如果你已经使用的代码应该工作:=

Canvas.SetZIndex((Tile)sender, 99); 

除非您点击其他图块,否则它们最终会得到99 Z-Index。你需要的是保持使用的最后一个Zindex值,然后在每次事件发生时递增并使用它的值。

这个答案可能没有帮助,因为所有假设中的一个或多个可能是错误的。如果您在问题中更具描述性,则可以找到更好的匹配答案。

相关问题