1

I have an Image control. When the image is clicked at runtime it should be added to a grid as a child. I have implemented this.

My requirement now is when the image (which is now inside a grid) is tapped I need a border around the image. How can I achieve this?

I have added the following code inside image tapped but cannot see the border

Border b = new Border(); 
b.BorderThickness = new Thickness(4); 
img1 = new Image(); 
img1.MaxHeight = 300; 
img1.MaxWidth = 500; 
b.Child = img1; 
img1.Source = new BitmapImage((new Uri(imgPath, UriKind.RelativeOrAbsolute)));
grid1.Children.Add(b);
4

1 回答 1

2

不要将图像作为网格单元子元素放置,而是创建一个 Border 对象并将图像作为内容放入其中。

最初,边框可以有 BorderBrush 透明和厚度 = 1 或其他。然后,将 Tapped 事件添加到图像中以修改边框画笔颜色。

根据上面的代码,您需要类似

img1.Tapped += delegate(object sender, EventArgs e)
{
    ((sender as Image).Parent as Border).BorderBrush = Brushes.Blue;
});
于 2012-05-03T03:39:59.703 回答