0

这在动态绑定图像时不起作用

Image Imgsource = new Image();

Imgsource.Source = new BitmapImage(new Uri("/Finder;component/Images/Chrysanthemum.png", UriKind.RelativeOrAbsolute));

提前致谢

4

4 回答 4

1

请试试这个...

  public void setimagebackgroud(string uri)
    {
        ImageBrush imageBrush = new ImageBrush();
        Image image = new Image();
        image.Source = new BitmapImage(new Uri(uri,UriKind.RelativeOrAbsolute));
        imageBrush.ImageSource = image.Source;
    }
于 2013-05-27T05:34:19.133 回答
1

您的代码创建一个Image元素。但是随后您需要将该元素添加到页面中的容器中。以LayoutRoot网格为例:

Image Imgsource = new Image();
Imgsource.Source = new BitmapImage(new Uri("/Finder;component/Images/Chrysanthemum.png", UriKind.RelativeOrAbsolute));
this.LayoutRoot.Children.Add(Imgsource);
于 2013-05-24T08:24:42.270 回答
1

您需要通过图像转换器绑定:

 public class ImageConverter : IValueConverter
 {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
           MemoryStream memStream = new MemoryStream((byte[])value,false);
           BitmapImage empImage = new BitmapImage();
           empImage.SetSource(memStream);
           return empImage; 
      }
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
           throw new NotImplementedException();
      }
 }
于 2013-05-26T10:20:44.483 回答
-2
 The binding as it should be done

 string s = "Hello";

 //Create the binding description   
 Binding b = new Binding("");   
 b.Mode = BindingMode.OneTime;   
 b.Source = s;

 //Attach the binding to the target  
 MyText.SetBinding(TextBlock.TextProperty, b);

 See if this helps
于 2013-05-24T08:32:11.713 回答