2013-11-20 3 views
0

Binding to Image.SourcePropertybyte[] 변수에서 사용하고 있습니다. IValueConverter에서 value.Length > 0이 맞는지 확인하고, 그렇다면 그 값에서 소스를 설정합니다. 그렇다면 설정되어 있다면 알 필요가 있으므로 클리어 버튼을 표시하거나 숨길 수 있습니다. Image.Source 항상 null이 아닙니다. 어떻게 알 수 있습니까? 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"; 
+0

같은 수 질문을 분명히 해 주시겠습니까? 나는 당신의 시나리오가 무엇인지 이해하지 못합니까? –

답변

0

뭔가 그

if (((BitmapImage)myImage.Source).UriSource == null) 
{ 
//Image is empty 
} 

또는

var str = ((BitmapImage)myImage.Source).UriSource != null ? "Image is set" : "Image is empty"; 
관련 문제