WPF MultiBinding VS设计器异常 [英] WPF MultiBinding VS designer exception

查看:89
本文介绍了WPF MultiBinding VS设计器异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Visual Studio 2010 designer says that unhandled exception occurred in MultiValueConverter but I can build my program and it works fine (multibinding also works).

XAML(我在构造函数中设置了window.DataContext) :

XAML (I set window.DataContext in constructor):

            <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 []值,类型targetType,Object参数,CultureInfo文化)在C:... \Converters.cs:line 176
在System.Windows.Data。在System.Windows.Data.MultiBindingExpression.Transfer()在System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings)
在System.Windows.Data处的MultiBindingExpression.TransferValue()
。 MultiBindingExpression.AttachToContext(Boolean lastChance)
在System.Windows.Data.MultiBindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
在MS.Internal.Data.DataBindEngine.Task.Run(布尔值lastChance)
,位于MS.Internal.Data.DataBindEngine.Run(对象arg)
,位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,Int32 numArgs)
MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象args,Int32 numArgs,委托ca tchHandler)

System.InvalidCastException Specified cast is not valid. at myassemblyname.MultiEnabledToEnabled.Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture) in C:...\Converters.cs:line 176 at System.Windows.Data.MultiBindingExpression.TransferValue() at System.Windows.Data.MultiBindingExpression.Transfer() at System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings) at System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance) at System.Windows.Data.MultiBindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance) at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) at MS.Internal.Data.DataBindEngine.Run(Object arg) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

推荐答案

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

My best guess is that the binding happens before some initialization, and at least one value in the collection of object is DependencyProperty.UnsetValue, making the cast invalid.

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

Now, assuming you have a design time viewmodel set up, you could check beforehand if all the values are indeed booleans:

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

但是,一旦任何视图变得复杂,设计器就会中断,让它重新工作很痛苦。

But as soon as any view gets complicated, the designer breaks, and it is painful to get it working again.

Expression Blend可以更好地解决这个问题,如果您绝对想要设计师,但又不想打扰设置设计时环境,那就去吧。

Expression Blend handles this better, if you absolutely want a designer but can't be bothered to set up a design time environment, go for it.

否则,就像大多数人一样:忘记设计师。

Otherwise do it like most people: forget about the designer.

这篇关于WPF MultiBinding VS设计器异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆