1

Visual Studio 2010 デザイナーは、MultiValueConverter で未処理の例外が発生したと言っていますが、プログラムをビルドでき、正常に動作します (マルチバインディングも動作します)。

ここに画像の説明を入力

XAML (コンストラクターで window.DataContext を設定):

            <ComboBox Name="cbbProfile" DisplayMemberPath="Name" Grid.Row="1" Margin="10,5" Grid.ColumnSpan="3" ItemsSource="{Binding ProfilesData.ProfilesItems}" SelectionChanged="cbbProfile_SelectionChanged" >
                <ComboBox.IsEnabled>
                    <MultiBinding Converter="{StaticResource multiEnabledToEnabled}">
                        <Binding Path="ProfilesData.ProfilesItems.Count" Converter="{StaticResource itemsCountToEnabled}" />
                        <Binding Path="State" Converter="{StaticResource stateToControlsEnabled}" />
                    </MultiBinding>
                </ComboBox.IsEnabled>
            </ComboBox>

コンバーター:

public class MultiEnabledToEnabled : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    { 
        foreach (object val in values)
        {
            if (!(bool) val)     // <-- EXCEPTION (line 176) HERE 
                return false;
        } 

        return true;
    }    

public class ItemsCountToEnabled : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (int)value == 0 ? false : true;
    }

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

public class StateToControlsEnabled : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var val = (ProgramState)value;
        switch (val)
        {
            ...
            default:
                return true;
        }

    }

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

VS 例外テキスト:

System.InvalidCastException 指定されたキャストは無効です。System.Windows の System.Windows.Data.MultiBindingExpression.TransferValue() の C:...\Converters.cs:line 176 の myassemblyname.MultiEnabledToEnabled.Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture) でSystem.Windows.Data.MultiBindingExpression.UpdateTarget の .Data.MultiBindingExpression.Transfer() (Boolean includeInnerBindings) System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance) の System.Windows.Data.MultiBindingExpression.MS.Internal.Data System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate) で MS.Internal.Data.DataBindEngine.Run(Object arg) で MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) で .IDataBindEngineClient.AttachToContext(Boolean lastChance)コールバック、オブジェクト引数、

4

2 に答える 2

2

VS デザイナーは扱うのが難しい獣であり、努力する価値がないという結論に達しました。ただし、次を使用できます。

if(DesignerProperties.GetIsInDesignMode(Application.MainWindow))

コンバーターのデフォルト値を提供します。これにより、エラーが除去されます。

msdn の DesignerProperties.GetIsInDesignMode メソッド

于 2012-10-01T09:51:59.330 に答える
2

私の最善の推測は、初期化の前にバインディングが発生し、 object のコレクション内の少なくとも 1 つの値が でDependencyProperty.UnsetValueあり、キャストが無効になっていることです。

ここで、設計時のビューモデルが設定されていると仮定すると、すべての値が実際にブール値であるかどうかを事前に確認できます。

if(values.All(v => v is bool))
{
   //Do regular computation
}
else
{
   //Handle edge case
}

しかし、ビューが複雑になるとすぐに、デザイナーは機能しなくなり、再び機能させるのは苦痛になります。

Expression Blend はこれをより適切に処理します。どうしてもデザイナーが必要であるが、デザイン時の環境をセットアップするのが面倒な場合は、それを選択してください。

それ以外の場合は、ほとんどの人と同じように、デザイナーのことは忘れてください。

于 2012-10-01T09:50:35.213 に答える