2012-03-03 8 views
2

IValueConverterを使用すると、同じ値を返すIMultiValueConverterが返されますが、それはなぜですか?なぜマルチバインディングがCornerRadiusで機能しないのですか

<Border Background="Red" Width="100" Height="100" 
     CornerRadius="{Binding Converter={vc:SingleAndMultiConverter}}" /> 
<Border Background="Red" Width="100" Height="100" 
     CornerRadius="{MultiBinding Converter={vc:SingleAndMultiConverter}}" /> 
public class SingleAndMultiConverter : MarkupExtension, IValueConverter, IMultiValueConverter 
{ 
    public override object ProvideValue(IServiceProvider serviceProvider) 
    { 
     return this; 
    } 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return Convert(); 
    } 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return Convert(); 
    } 
    private object Convert() 
    { 
     return 15; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

多結合このエラーをスロー:

Value produced by BindingExpression is not valid for target property.; Value='15'

+0

は、先生はちょっと –

+0

...あなたが何か間違ったことをやっているが、私達に関連するコードを示していないと述べ、これはほぼすべての私のコードです。 あなたがコードを読むには、ideで生成してください。 – GeminiYellow

+0

今、このトラブルから私を助けてくれますか? – GeminiYellow

答えて

0

H.B. 1

[ValueConversion(typeof(object[]),typeof(CornerRadius))] 
public class Multi : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     return new CornerRadius(Double.Parse("15")); 
    } 

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

} 
+1

15を最初に文字列にし、次にそれを再度解析する背後にあるロジックは何ですか? (元のコードでは文字列ではありませんでした) –

+0

ああ、それは矛盾した結果なので、私は混乱しています。 しかし、[属性]は仕事です。 – GeminiYellow

+0

私がコメントで言ったように、マルチは文字列を返していたが、シングルはそうではなかったと思う。私はタイピングを明確にするために二重にそのctorを使用しました。 – Paparazzi

2

Border.CornerRadiusCornerRadiusです。値コンバーターは常にプロパティの正しい型を返す必要があります。

理由が分からない理由でマルチバインディングを使用する場合、type convertersを使用してデフォルト値変換が行われていない可能性があります。あなたがソースコードを掘り起こすなら、あなたは何かを見つけるかもしれないが、それはおそらく楽しい旅にはならないだろう。

+0

私はOPによって気づかれたのとまったく同じ観察をしました。 'IValueConverter'を使って値を変換した後、' IMultiValueConverter'の後ではなく、デフォルトの型変換器を呼び出すことができますか? :/私は、この文書化されていない振る舞いによってちょっと困惑しています。 –

関連する問題