共用方式為


Panel.ZIndex 附加屬性

定義

取得或設定代表元素在 Z 平面出現的順序。

see GetZIndex, and SetZIndex
see GetZIndex, and SetZIndex
see GetZIndex, and SetZIndex

範例

下列範例示範如何使用 Extensible Application Markup Language (XAML) 和程式碼來設定 屬性的值 ZIndex


// Create the application's main window
mainWindow = gcnew Window();
mainWindow->Title = "Canvas ZIndex Sample";

// Create the Canvas
myCanvas = gcnew Canvas();

// Create the child Rectangle elements
myRectangle1 = gcnew Rectangle();
myRectangle2 = gcnew Rectangle();
myRectangle3 = gcnew Rectangle();
myRectangle4 = gcnew Rectangle();
myRectangle5 = gcnew Rectangle();
myRectangle6 = gcnew Rectangle();

// Set properties on the Rectangle elements
Canvas::SetTop(myRectangle1, 100);
Canvas::SetLeft(myRectangle1, 100);
Canvas::SetZIndex(myRectangle1, 3);
myRectangle1->Fill = Brushes::Blue;
myRectangle1->Width = 100;
myRectangle1->Height = 100;

Canvas::SetTop(myRectangle2, 150);
Canvas::SetLeft(myRectangle2, 150);
Canvas::SetZIndex(myRectangle2, 1);
myRectangle2->Fill = Brushes::Yellow;
myRectangle2->Width = 100;
myRectangle2->Height = 100;

Canvas::SetTop(myRectangle3, 200);
Canvas::SetLeft(myRectangle3, 200);
Canvas::SetZIndex(myRectangle3, 2);
myRectangle3->Fill = Brushes::Green;
myRectangle3->Width = 100;
myRectangle3->Height = 100;

Canvas::SetTop(myRectangle4, 300);
Canvas::SetLeft(myRectangle4, 200);
Canvas::SetZIndex(myRectangle4, 1);
myRectangle4->Fill = Brushes::Green;
myRectangle4->Width = 100;
myRectangle4->Height = 100;

Canvas::SetTop(myRectangle5, 350);
Canvas::SetLeft(myRectangle5, 150);
Canvas::SetZIndex(myRectangle5, 3);
myRectangle5->Fill = Brushes::Yellow;
myRectangle5->Width = 100;
myRectangle5->Height = 100;

Canvas::SetTop(myRectangle6, 400);
Canvas::SetLeft(myRectangle6, 100);
Canvas::SetZIndex(myRectangle6, 2);
myRectangle6->Fill = Brushes::Blue;
myRectangle6->Width = 100;
myRectangle6->Height = 100;

// Add the Rectangles to the Canvas' Children collection
myCanvas->Children->Add(myRectangle1);
myCanvas->Children->Add(myRectangle2);
myCanvas->Children->Add(myRectangle3);
myCanvas->Children->Add(myRectangle4);
myCanvas->Children->Add(myRectangle5);
myCanvas->Children->Add(myRectangle6);

// Add the Canvas as the Content of the parent Window Object
mainWindow->Content = myCanvas;
mainWindow->Show();

// Create the application's main window
mainWindow = new Window ();
mainWindow.Title = "Canvas ZIndex Sample";

// Create the Canvas
myCanvas = new Canvas();

// Create the child Rectangle elements
myRectangle1 = new Rectangle();
myRectangle2 = new Rectangle();
myRectangle3 = new Rectangle();
myRectangle4 = new Rectangle();
myRectangle5 = new Rectangle();
myRectangle6 = new Rectangle();

// Set properties on the Rectangle elements
Canvas.SetTop(myRectangle1, 100);
Canvas.SetLeft(myRectangle1, 100);
Canvas.SetZIndex(myRectangle1, 3);
myRectangle1.Fill = Brushes.Blue;
myRectangle1.Width = 100;
myRectangle1.Height = 100;

Canvas.SetTop(myRectangle2, 150);
Canvas.SetLeft(myRectangle2, 150);
Canvas.SetZIndex(myRectangle2, 1);
myRectangle2.Fill = Brushes.Yellow;
myRectangle2.Width = 100;
myRectangle2.Height = 100;

Canvas.SetTop(myRectangle3, 200);
Canvas.SetLeft(myRectangle3, 200);
Canvas.SetZIndex(myRectangle3, 2);
myRectangle3.Fill = Brushes.Green;
myRectangle3.Width = 100;
myRectangle3.Height = 100;

Canvas.SetTop(myRectangle4, 300);
Canvas.SetLeft(myRectangle4, 200);
Canvas.SetZIndex(myRectangle4, 1);
myRectangle4.Fill = Brushes.Green;
myRectangle4.Width = 100;
myRectangle4.Height = 100;

Canvas.SetTop(myRectangle5, 350);
Canvas.SetLeft(myRectangle5, 150);
Canvas.SetZIndex(myRectangle5, 3);
myRectangle5.Fill = Brushes.Yellow;
myRectangle5.Width = 100;
myRectangle5.Height = 100;

Canvas.SetTop(myRectangle6, 400);
Canvas.SetLeft(myRectangle6, 100);
Canvas.SetZIndex(myRectangle6, 2);
myRectangle6.Fill = Brushes.Blue;
myRectangle6.Width = 100;
myRectangle6.Height = 100;

// Add the Rectangles to the Canvas' Children collection
myCanvas.Children.Add(myRectangle1);
myCanvas.Children.Add(myRectangle2);
myCanvas.Children.Add(myRectangle3);
myCanvas.Children.Add(myRectangle4);
myCanvas.Children.Add(myRectangle5);
myCanvas.Children.Add(myRectangle6);

// Add the Canvas as the Content of the parent Window Object
mainWindow.Content = myCanvas;
mainWindow.Show ();
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="ZIndex Sample">
  <Canvas>
    <Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="100" Canvas.Left="100" Fill="blue"/>
    <Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="150" Canvas.Left="150" Fill="yellow"/>
    <Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="200" Canvas.Left="200" Fill="green"/>

    <!-- Reverse the order to illustrate z-index property -->

    <Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="300" Canvas.Left="200" Fill="green"/>
    <Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="350" Canvas.Left="150" Fill="yellow"/>
    <Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="400" Canvas.Left="100" Fill="blue"/>
  </Canvas>
</Page>

備註

指定 element 之 的 愈大 value ,較有可能 element 出現在前景。 同樣地,如果 element 的相對低 value ,可能會 element 出現在背景中。 例如, element 具有 5 的 value 會出現在 值為 4 的 上方 element value ,接著會出現在值為 3 的 上方 element ,依此類傳。 允許負值,而且會繼續此優先順序模式。

具有相等 ZIndex 值之集合的成員 Children 會依出現在視覺化樹狀結構中的順序呈現。 您可以逐一查看集合的成員來判斷子系的 Children 索引位置。

相依性屬性資訊

識別碼欄位 ZIndexProperty
設定為 的中繼資料屬性 true

適用於