2012-03-09 15 views
30

この属性のポイントは何ですか?それを追加した後、私はまだ値オブジェクトのキャストを行う必要があります。ValueConversionAttributeクラスのポイントは?

[ValueConversion(sourceType: typeof(double), targetType: typeof(string))] 
public class SpeedConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var speed = (double)value; 

コードの可読性のみですか? xamlの文字列へのバインディングのパスを変更すると、Visual Studioは間違った型に関する警告を出さないので、キャスト時に例外がスローされるため、コンパイル中にキャッチした初期のエラーでさえも意味しません。 キャストを文字列に変更することもできます。この属性と競合しても警告はスローされません。

答えて

23

あなたは可能性がありますValueConversionAttributeを使用して、コンバータにどのようなタイプが関係しているかを判断し、その情報を有効に使用してください。 ValueConversionAttributeの使用例として、Piping Value Converters in WPFを見てください。

この例では、複数のコンバータクラスをチェーンする方法を示し、ValueConversionを使用して、型情報を次のコンバータの行に渡すことができます。

[ValueConversion(typeof(string), typeof(ProcessingState))] 
public class IntegerStringToProcessingStateConverter : IValueConverter 
{ 
object IValueConverter.Convert( 
    object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    int state; 
    bool numeric = Int32.TryParse(value as string, out state); 
    Debug.Assert(numeric, "value should be a String which contains a number"); 
    Debug.Assert(targetType.IsAssignableFrom(typeof(ProcessingState)), 
    "targetType should be ProcessingState"); 

    switch(state) 
    { 
    case -1: 
    return ProcessingState.Complete; 
    case 0: 
    return ProcessingState.Pending; 
    case +1: 
    return ProcessingState.Active; 
    } 
    return ProcessingState.Unknown; 
} 

object IValueConverter.ConvertBack( 
    object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    throw new NotSupportedException("ConvertBack not supported."); 
} 
} 
// ************************************************************* 
[ValueConversion(typeof(ProcessingState), typeof(Color))] 
public class ProcessingStateToColorConverter : IValueConverter 
{ 
object IValueConverter.Convert( 
    object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    Debug.Assert(value is ProcessingState, "value should be a ProcessingState"); 
    Debug.Assert(targetType == typeof(Color), "targetType should be Color"); 

    switch((ProcessingState)value) 
    { 
    case ProcessingState.Pending: 
    return Colors.Red; 
    case ProcessingState.Complete: 
    return Colors.Gold; 
    case ProcessingState.Active: 
    return Colors.Green; 
    } 
    return Colors.Transparent; 
} 

object IValueConverter.ConvertBack( 
    object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    throw new NotSupportedException("ConvertBack not supported."); 
} 
} 

object IValueConverter.Convert( 
    object value, Type targetType, object parameter, CultureInfo culture) 
{ 
object output = value; 
for(int i = 0; i < this.Converters.Count; ++i) 
{ 
    IValueConverter converter = this.Converters[i]; 
    Type currentTargetType = this.GetTargetType(i, targetType, true); 
    output = converter.Convert(output, currentTargetType, parameter, culture); 

    // If the converter returns 'DoNothing' 
    // then the binding operation should terminate. 
    if(output == Binding.DoNothing) 
    break; 
} 
return output; 
} 
//***********Usage in XAML************* 
    <!-- Converts the Status attribute text to a Color --> 
    <local:ValueConverterGroup x:Key="statusForegroundGroup"> 
      <local:IntegerStringToProcessingStateConverter /> 
      <local:ProcessingStateToColorConverter /> 
    </local:ValueConverterGroup> 
+2

IMHO、プログラムの必要性/属性の使用状況を示すため、これが答えになります。素晴らしい発見。 – MarqueIV

12

単なる注釈です。

MSDN

IValueConverterインターフェイスを実装する場合、開発ツールに変換

に関連するデータの種類を示すために ValueConversionAttribute属性で実装を飾るために良い方法です

"開発ツール"がその情報で何をするのか分かりません...

+0

このような情報をコメントに書き込む方が簡単です。たぶんそれは自動生成ドキュメントに使用されますか? – Lars

+0

@Lars:誰もが知っている、まだどこでも役に立つとは思わなかった... –

+8

Uh-huh ...私の人生で目的を持っている - これを利用するためのVisual Studioアドインを書く属性。 – Lars

関連する問題