Wpf 如何绑定图像源?
从一些例子来看,我认为我做得对,但它不起作用,所以我想在这里查看一下。我正在绑定图像源,但图像不在那里。如果我去掉绑定,只需将图像源设置为下面方法中使用的路径,就可以了Wpf 如何绑定图像源?,wpf,image,binding,Wpf,Image,Binding,从一些例子来看,我认为我做得对,但它不起作用,所以我想在这里查看一下。我正在绑定图像源,但图像不在那里。如果我去掉绑定,只需将图像源设置为下面方法中使用的路径,就可以了 public Image myImage = new Image(); public void someMethod() { BitmapImage biSource = new BitmapImage(); biSource.BeginInit(); biSource.CacheOption = Bi
public Image myImage = new Image();
public void someMethod() {
BitmapImage biSource = new BitmapImage();
biSource.BeginInit();
biSource.CacheOption = BitmapCacheOption.OnLoad;
biSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
biSource.UriSource = new Uri(@"C:\Images\testimage.jpg");
biSource.DecodePixelWidth = 150;
biSource.EndInit();
myImage.Source = biSource;
}
xaml code
<Image Source="{Binding Path=myImage}" Width="150" Height="150" />
public Image myImage=new Image();
公共方法(){
BitmapImage biSource=新的BitmapImage();
biSource.BeginInit();
biSource.CacheOption=BitmapCacheOption.OnLoad;
biSource.CreateOptions=BitmapCreateOptions.IgnoreImageCache;
biSource.UriSource=新Uri(@“C:\Images\testimage.jpg”);
biSource.DecodePixelWidth=150;
biSource.EndInit();
myImage.Source=biSource;
}
xaml代码
您试图将图像
绑定到图像
的源,但无法正常工作。您需要将位图图像
绑定到图像
的源
另外,BitmapImage
需要作为公共属性公开。如果您是数据绑定新手,请阅读
进一步了解。您应该将
源
属性绑定到图像源
,而不是图像
。将biSource
作为属性公开并绑定到它,而不是myImage