如何在Silverlight Canvas控件上获取或设置图像的位置

时间:2011-01-21 12:41:49

标签: c# silverlight

我有一个Image控件作为Canvas控件的子控件。如果我使用位图加载Image控件并将Stretch属性设置为Uniform,我会得到一个居中的图像。但是,我无法设置或检索图像的位置。解决任何人?

以下是一些使问题更容易理解的代码:

Image img = new Image();
img.Height = maxSize.Height;
img.Width = maxSize.Width;
img.Stretch = Stretch.Uniform;
img.Source = bmp;
myCanvas.Children.Add(img);

// returns 0:
MessageBox.Show(((Image)myCanvas.Children[0]).GetValue(Canvas.LeftProperty).ToString());
MessageBox.Show(((Image)myCanvas.Children[0]).GetValue(Canvas.TopProperty).ToString());

// in order to set explicitly set the values I have to know what they are?
Double left = {the current left value};
Double top = {the current top value};
((Image)myCanvas.Children[0]).SetValue(Canvas.LeftProperty, left);
((Image)myCanvas.Children[0]).SetValue(Canvas.TopProperty, top);`

我选择代码然后按下{}按钮,但它对我不起作用,也不会手动输入代码。

1 个答案:

答案 0 :(得分:3)

使用Canvas.LeftProperty和Canvas.TopProperty

var left = (double)image.GetValue(Canvas.LeftProperty)
left += 50;
image.SetValue(Canvas.LeftProperty, left);