2011-01-16 32 views
4

私は画像コントロールでマルチバインドしています。私はboolの1つのタイプ(IsLogged)と1つのtypeof Uri(ProfilePhoto)の2つのプロパティをバインドします。ImageSourceのコンバーター - 画像をグレースケールに変換

XAML:

       <Image.Source > 
            <MultiBinding Converter="{StaticResource avatarConverter}"> 
             <Binding Path="ProfilePhoto"></Binding> 
             <Binding Path="StatusInfo.IsLogged"></Binding> 
            </MultiBinding> 
           </Image.Source> 
          </Image> 

私はプロパティIsLoggedがfalseの場合のBitmapImageはグレースケールに変換するコンバータを、作成します。

それは次のようになります。

public class AvatarConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     var image = values[0] as BitmapImage; 

     string s = values[1].ToString(); 

     bool isLogged = System.Convert.ToBoolean(s); 

     if (!isLogged) 
     { 
      try 
      { 
       if (image != null) 
       { 
        var grayBitmapSource = new FormatConvertedBitmap(); 
        grayBitmapSource.BeginInit(); 
        grayBitmapSource.Source = image; 
        grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float; 
        grayBitmapSource.EndInit(); 
        return grayBitmapSource; 
       } 
       return null; 

      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 
     return image; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

だけ私はBitmapImageのFOイメージソースプロパティのタイプに結合するが、私はウリのプロパティタイプをバインドする必要がある場合、それは良い作品。

私はコンバータの作成変数BitmapImageとソースとしてUriを使用することを恐れています。 この変数をイメージのソースとして返します。私はこれが理想的な方法ではないと思う。たぶん私は間違っています。

あなたの意見は何ですか

いくつかのエレガントなソリューションですか?

答えて

10

これはコンバーターでも可能ですが、はるかに優れたオプションがあります:シェーダーエフェクトを使用することです。 this pageにGreyscaleEffectの実装があります。

+0

面白い解決策をありがとう、私はそれを試してみます。 –

+0

xml名前空間は – LordWilmore

+0

@LordWilmoreにマッピングされた 'fx'です。私はGreyscaleEffectクラスを取ったページはもはや利用できません。 –

関連する問題