Skip to main content
 首页 » 编程设计

c#之WPF MultiBinding VS 设计器异常

2024年02月20日10cmt

Visual Studio 2010 设计器说 MultiValueConverter 中发生了未处理的异常,但我可以构建我的程序并且它工作正常(多绑定(bind)也可以)。

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 指定的转换无效。 在 myassemblyname.MultiEnabledToEnabled.Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture) 在 C:...\Converters.cs:line 176 在 System.Windows.Data.MultiBindingExpression.TransferValue() 在 System.Windows.Data.MultiBindingExpression.Transfer() 在 System.Windows.Data.MultiBindingExpression.UpdateTarget( bool 值 includeInnerBindings) 在 System.Windows.Data.MultiBindingExpression.AttachToContext( bool 最后机会) 在 System.Windows.Data.MultiBindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext( bool 最后机会) 在 MS.Internal.Data.DataBindEngine.Task.Run( bool 最后机会) 在 MS.Internal.Data.DataBindEngine.Run(对象参数) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托(delegate)回调,对象参数,Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源、委托(delegate)方法、对象参数、Int32 numArgs、委托(delegate) catchHandler)

请您参考如下方法:

我最好的猜测是绑定(bind)发生在某些初始化之前,对象集合中至少有一个值是 DependencyProperty.UnsetValue,这使得转换无效。

现在,假设您设置了设计时 View 模型,您可以事先检查所有值是否确实是 bool 值:

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

但是一旦任何 View 变得复杂,设计器就会崩溃,并且很难让它重新工作。

Expression Blend 可以更好地处理这个问题,如果您绝对想要一个设计师但又懒得设置设计时间环境,那就去吧。

否则就和大多数人一样:忘掉设计师吧。