分层时尚的UIElement

时间:2010-09-09 09:51:54

标签: c# .net silverlight windows-phone-7 uielement

技术:Silverlight 平台:Windows Phone 7

foreach(Rectangle rec in listrect)
{
    Layoutroot.Children.Add(rec);
}

完成一些鼠标移动捕获事件并使这些矩形可拖动。

我想,每个子元素都会以某种分层的方式添加到布局中,其中第一个矩形接下来位于底层,然后打开和打开。

如何避免这种情况并将所有UIElement放在同一平面上,而不管它们被添加到画布的子级的顺序如何?

1 个答案:

答案 0 :(得分:0)

Silverlight使用不同的ZIndex绘制每个元素。如果您未指定ZIndex,则Silverlight会假定添加的最新元素具有更高的隐含ZIndex。 XAML也是如此 - 其他元素下面的元素具有更高的隐含ZIndex

如果要在示例中控制Rect元素的绘制,则需要手动修改ZIndex。首先,确保您的Layoutroot元素是Canvas。接下来,在鼠标移动事件处理程序中,将ZIndex依赖项属性更改为更高。

例如,此示例将在拖动选定矩形时将其置于最前端。

<phone:PhoneApplicationPage x:Class="WindowsPhoneApplication1.MainPage"
                            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                            xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone">
    <Canvas x:Name="LayoutRoot" Background="Transparent" />
</phone:PhoneApplicationPage>

背后的代码:

using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace WindowsPhoneApplication1
{
    public partial class MainPage : PhoneApplicationPage
    {
        private readonly Random _rnd = new Random();
        private List<Rectangle> _listRect;
        private Rectangle _selectedRect;
        private int _selectedRectPrevZIndex;
        public MainPage()
        {
            InitializeComponent();
            LayoutRoot.MouseMove += Rectangle_MouseMove;
            CreateAndAddRectangles();
            AddRectangles();
        }
        private void CreateAndAddRectangles()
        {
            _listRect = new List<Rectangle>(10);
            for (var i = 0; i < 10; i++)
            {
                var rec = new Rectangle {Height = 50, Width = 50};
                rec.SetValue(Canvas.LeftProperty, i * 30d);
                rec.SetValue(Canvas.TopProperty, i * 30d);
                rec.Fill = new SolidColorBrush(
                    Color.FromArgb(255,
                                   Convert.ToByte(_rnd.NextDouble() * 255),
                                   Convert.ToByte(_rnd.NextDouble() * 255),
                                   Convert.ToByte(_rnd.NextDouble() * 255)));
                rec.MouseLeftButtonDown += Rectangle_MouseLeftButtonDown;
                rec.MouseLeftButtonUp += Rectangle_MouseLeftButtonUp;
                rec.MouseMove += Rectangle_MouseMove;
                _listRect.Add(rec);
            }
        }
        private void AddRectangles()
        {
            foreach (var rec in _listRect)
                LayoutRoot.Children.Add(rec);
        }
        private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var rec = sender as Rectangle;
            if (rec == null)
                return;

            _selectedRectPrevZIndex = (int)rec.GetValue(Canvas.ZIndexProperty);
            System.Diagnostics.Debug.WriteLine(_selectedRectPrevZIndex);
            rec.SetValue(Canvas.ZIndexProperty, 100);
            _selectedRect = rec;
        }
        private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var rec = sender as Rectangle;
            if (rec == null || _selectedRect == null)
                return;

            rec.SetValue(Canvas.ZIndexProperty, _selectedRectPrevZIndex);
            _selectedRect = null;
        }
        void Rectangle_MouseMove(object sender, MouseEventArgs e)
        {
            if (_selectedRect == null)
                return;

            var pos = e.GetPosition(LayoutRoot);
            _selectedRect.SetValue(Canvas.LeftProperty, pos.X);
            _selectedRect.SetValue(Canvas.TopProperty, pos.Y);
        }
    }
}