这在动态绑定图像时不起作用
Image Imgsource = new Image();
Imgsource.Source = new BitmapImage(new Uri("/Finder;component/Images/Chrysanthemum.png", UriKind.RelativeOrAbsolute));
提前致谢
这在动态绑定图像时不起作用
Image Imgsource = new Image();
Imgsource.Source = new BitmapImage(new Uri("/Finder;component/Images/Chrysanthemum.png", UriKind.RelativeOrAbsolute));
提前致谢
请试试这个...
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;
}
您的代码创建一个Image
元素。但是随后您需要将该元素添加到页面中的容器中。以LayoutRoot
网格为例:
Image Imgsource = new Image();
Imgsource.Source = new BitmapImage(new Uri("/Finder;component/Images/Chrysanthemum.png", UriKind.RelativeOrAbsolute));
this.LayoutRoot.Children.Add(Imgsource);
您需要通过图像转换器绑定:
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();
}
}
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