1
我想在wpf应用程序中绑定图像。我正在使用vs2010。WPF图像绑定和INotifyPropertyChanged,与图像显示问题
我粘贴下面的代码,并解释我做了什么,什么起作用,什么不起作用。
XAML代码:
<Image Name="newImage" ImageFailed="newImage_ImageFailed" HorizontalAlignment="Right" Width="auto" Height="auto" Margin="5" Source="{Binding imgSource}">
C#代码:下面
public MainWindow()
{
InitializeComponent();
arraytoImage atim = new arraytoImage();
newImage.DataContext = atim;
}
代码是在不同的命名空间,其中arraytoImage
类被实现。这个类需要一个cuda数组,创建一个位图,然后使用memorystream将其转换为bitmapimage。目前,我正在为所有像素设置随机颜色,以查看该绑定是否有效。但事实并非如此。下面我粘贴了一个显示图片的代码。
我相信bitmapimage是正确创建的。我认为这个问题是不正确的绑定。
class arraytoImage : INotifyPropertyChanged
{
// displays images (focused files)
private BitmapImage bitmapImage = new BitmapImage();
private BitmapImage testim = new BitmapImage();
public BitmapImage arraytoImageCon(cuFloatComplex[] dataIn, int wid, int ht)
{
//code that generates bitmapimage
}
public BitmapImage imgSource
{
get { return testim1; }
set
{
if (testim1 != value)
{
testim1 = value;
OnPropertyChanged("imgSource");
}
}
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
编辑:调用arrayToImageCon:
public class ReadRawFiles
{
//Tons of code
public void focusdata()
{
//tons of code
arraytoImage atoi = new arraytoImage();
BitmapImage tmp= atoi.arraytoImageCon(datafft_azi, nazimuth,nrange);
atoi.imgSource=tmp;
}
}
我的问题是,我在做什么错。
非常感谢。如果我错过了一些东西,请询问更多细节。
Regards
你的代码有点混乱。为什么不直接删除testim1并返回bitmapImage。你也可以在哪里调用arrayToImage con?你为什么不直接在那里打电话给这位二传手,这样财产得到适当的提升? – dowhilefor
尝试'test = bitmapImage;'而不是'testim1 = bitmapImage;' –
@dowhile因其杂乱,因为我处于学习的初始阶段。请稍微详细一点。谢谢 – Naresh