2016-06-18 15 views
0

XAML解析例外 "BitmapImage UriSourceを設定する必要があります"があります。解析中、私のコンバータは作成されますが、Convert()メソッドは呼び出されません。私は間違って何をしていますか?C#Wpfカスタムコンバータが作成されたが呼び出されなかった

XAML:

<ImageBrush > 
    <ImageBrush.ImageSource> 
      <BitmapImage UriSource="{Binding Path=Value.Image, Converter={StaticResource imageConverter}, ConverterParameter=Value.Image}" CacheOption="OnLoad"></BitmapImage> 
    </ImageBrush.ImageSource> 
</ImageBrush> 

のC#:

public class ImageConverter : IValueConverter 
{ 
    public ImageConverter() 
    { 
    } 
    public object Convert(object value, Type targetType, 
         object parameter, CultureInfo culture) 
    { 
     try 
     { 
      return new BitmapImage(new Uri((string)value)); 
     } 
     catch 
     { 
      return new BitmapImage(); 
     } 
    } 

    public object ConvertBack(object value, Type targetType, 
           object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

[ValueConversion(typeof(Uri), typeof(BitmapImage))] public class ImageConverter : IValueConverter { public ImageConverter() { } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { try { if(value == null) { return null; } var image = new BitmapImage(); image.BeginInit(); image.UriSource = value is Uri ? (Uri)value : new Uri(value.ToString()); image.CacheOption = BitmapCacheOption.OnLoad; image.EndInit(); return image; } catch { return null; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException("Convert back function is not supported"); } } 

とXAML:私は<BitmapImage>だけなので、私の地元のソースファイルがロックされないCacheOption.OnLoadを設定するために、私はカスタムImageConverterを作成したを使用するために必要なので、 'または' RelativeSource'プロパティを使用する場合、ImageBrush(または親コントロールの1つ)を含むコントロールの 'DataContext'プロパティを、' Value'プロパティを持つクラスのインスタンスに設定する必要があります。 Visual Studioの出力ウィンドウにバインディングエラーメッセージが表示されます。 – Clemens

答えて

0

私は解決策のようなものを見つけました。あなたが明示的に `ソースを設定しない限り

<ImageBrush ImageSource="{Binding Path=Value.Image, Converter={StaticResource imageConverter}}"> 
</ImageBrush> 
関連する問題