WP7备注(23)(Canvas改变Size|ZIndex|Canvas Touch)

首先,Canvas有四个Attached属性:Left,Top,Right,Buttom,它们的赋值,是在子项生成的时候,以Canvas的SetXxx的形式的方法赋值上去的,所以当Canvas改变大小的时候,也要重新更新这些Attached属性:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Canvas Name="canvas"
SizeChanged="OnCanvasSizeChanged" />
</Grid>
void OnCanvasSizeChanged(object sender, SizeChangedEventArgs args)
{
canvas.Children.Clear();
for (double y = 0; y < args.NewSize.Height; y += 75)
for (double x = 0; x < args.NewSize.Width; x += 75)
{
Ellipse ellipse = new Ellipse
{
Width = 100,
Height = 100,
Stroke = this.Resources["PhoneAccentBrush"] as Brush,
StrokeThickness = 10
};
Canvas.SetLeft(ellipse, x);
Canvas.SetTop(ellipse, y);
canvas.Children.Add(ellipse);
}
}

如上方代码所示

Canvas.SetLeft(ellipse, x);

Canvas.SetTop(ellipse, y);

重新制定Child的位置

同时,还有一些类似功能的代码:

ellipse.SetValue(Canvas.LeftProperty, x);
ellipse.SetValue(Canvas.TopProperty, y);

double x = GetLeft(child);
double y = GetTop(child);

double x = (double)child.GetValue(LeftProperty);
double y = (double)child.GetValue(TopProperty);

----------------------------------------------------------------------

还有个Attached属性是ZIndex,不多解释了

----------------------------------------------------------------------

protected override void OnManipulationStarted(ManipulationStartedEventArgs args)
{
args.ManipulationContainer = canvas;
base.OnManipulationStarted(args);
}

protected override void OnManipulationDelta(ManipulationDeltaEventArgs args)
{
UIElement element = args.OriginalSource as UIElement;
Point translation = args.DeltaManipulation.Translation;
Canvas.SetLeft(element, Canvas.GetLeft(element) + translation.X);
Canvas.SetTop(element, Canvas.GetTop(element) + translation.Y);
args.Handled = true;
base.OnManipulationDelta(args);
}
原文地址:https://www.cnblogs.com/otomii/p/2032618.html