简体   繁体   English

如何在WP7中将图像添加到Border对象

[英]How to add image to a Border object in WP7

In my windows phone app, I have a rectangular Border . 在我的Windows手机应用程序中,我有一个矩形Border I want to add image in the center of the border. 我想在边框的中心添加图像。 How to do that using C# ? 如何使用C#做到这一点?

Edit This code is not displaying any image. 编辑此代码不显示任何图像。 Any idea why ? 知道为什么吗?

Border b = (Border)FindName("border"+nom);
if (move == 1)
{
   var bi = new BitmapImage
   {
       UriSource = new Uri("/Images/smiley1.png", UriKind.Relative)
   };
   b.Child = new Image { Source = bi };
}

Edit 2 Apparently, correct syntax is UriSource = new Uri("/BoxIt;component/Images/smiley1.png", UriKind.Relative) This solves the problem :) 编辑2显然,正确的语法是UriSource = new Uri("/BoxIt;component/Images/smiley1.png", UriKind.Relative)这解决了问题:)

This is pretty straight forward. 这很简单。 You could have done a plain search. 你本可以做一个简单的搜索。 Anyways here's the solution you are looking for - 无论如何这里是您正在寻找的解决方案 -

In XAML - 在XAML中 -

<Border BorderThickness="2,2,2,2" BorderBrush="#FF000000" >

<Image x:Name="imgMainImage"  Visibility="Visible" Height="205" Width="180" />

</Border>

an Image can be adeed to a Border using following code: 使用以下代码可以将图像添加到边框

// Assume you have a border named Border1
Border1.Child = new Image() { /* ... */ };

Note: I do not recommend this. 注意:我不推荐这个。 DataTemplates with Data binding or TemplatedControls should be used when possible. 应尽可能使用DataTemplates with Data bindingTemplatedControls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM