【问题标题】:WPF MultiBinding VS designer exceptionWPF MultiBinding VS 设计器异常
【发布时间】:2012-09-22 03:44:58
【问题描述】:

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 指定的演员表无效。 在 C:...\Converters.cs:line 176 中的 myassemblyname.MultiEnabledToEnabled.Convert(Object[] values, Type targetType, Object parameter, CultureInfoculture) 在 System.Windows.Data.MultiBindingExpression.TransferValue() 在 System.Windows.Data.MultiBindingExpression.Transfer() 在 System.Windows.Data.MultiBindingExpression.UpdateTarget(布尔 includeInnerBindings) 在 System.Windows.Data.MultiBindingExpression.AttachToContext(布尔最后机会) 在 System.Windows.Data.MultiBindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(布尔最后机会) 在 MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) 在 MS.Internal.Data.DataBindEngine.Run(对象 arg) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象 args,Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

【问题讨论】:

    标签: c# wpf xaml data-binding multibinding


    【解决方案1】:

    VS 设计师是一个难以相处的野兽,我得出的结论是不值得付出努力。但你可以使用:

    if(DesignerProperties.GetIsInDesignMode(Application.MainWindow))
    

    为您的转换器提供默认值。这将消除错误。

    DesignerProperties.GetIsInDesignMode method at msdn

    【讨论】:

      【解决方案2】:

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

      现在,假设您设置了设计时视图模型,您可以预先检查所有值是否确实是布尔值:

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

      但是一旦任何视图变得复杂,设计师就会崩溃,并且让它再次工作很痛苦。

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

      否则会像大多数人那样做:忘记设计师。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-29
        • 2011-10-16
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多