0
我正在使用Binding
到Image.SourceProperty
從byte[]
變量。在IValueConverter
我檢查是否value.Length > 0
,如果是的話,我從它的值設置來源。然後我需要知道,如果它已設置,那麼我可以顯示或隱藏清除按鈕。 Image.Source
總是不爲空。如何知道,如果它是從byte[]
數組中設置的? 我的代碼:檢查ImageSource是否爲空
var bnd = new Binding
{
Mode = BindingMode.TwoWay,
Path = new PropertyPath("DataPath.Value"),
Converter = new ByteToImageConverter()
};
myImage.SetBinding(Image.SourceProperty, bnd);
public class ByteToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var val = value as byte[];
var bmp = new BitmapImage();
if (val.Length > 0) {
bmp.SetSource(new MemoryStream(val));
}
return bmp;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) {
return new byte[0];
}
var ms = new MemoryStream();
var bmp = new WriteableBitmap(value as BitmapSource);
bmp.SaveJpeg(ms, 150, 200, 0, 100);
return ms.ToArray();
}
}
現在我需要的代碼檢查,如果圖像信號源屬性設置:
// myImage.Source always != null even if there was no bmp.SetSource() call
var str = myImage.Source != null ? "Image is set" : "Image is empty";
燦你請澄清你的問題?我不確定我瞭解你的情況是什麼? –