Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF多绑定与设计器异常_C#_Wpf_Xaml_Data Binding_Multibinding - Fatal编程技术网

C# WPF多绑定与设计器异常

C# WPF多绑定与设计器异常,c#,wpf,xaml,data-binding,multibinding,C#,Wpf,Xaml,Data Binding,Multibinding,VisualStudio2010设计器说,多值转换器中发生了未经处理的异常,但我可以构建我的程序,并且它工作正常(多绑定也可以工作) XAML(我在构造函数中设置window.DataContext): 转换器: public class MultiEnabledToEnabled : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, Cu

VisualStudio2010设计器说,多值转换器中发生了未经处理的异常,但我可以构建我的程序,并且它工作正常(多绑定也可以工作)

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


转换器:

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();
    }
}
公共类MultiEnabledToEnabled:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{ 
foreach(值中的对象值)
{

如果(!(bool)val)/我的最佳猜测是绑定发生在某些初始化之前,并且对象集合中至少有一个值是
dependencProperty.UnsetValue
,这使得强制转换无效

现在,假设设置了设计时viewmodel,可以事先检查所有值是否都是布尔值:

if(values.All(v => v is bool))
{
   //Do regular computation
}
else
{
   //Handle edge case
}
但一旦任何视图变得复杂,设计师就会中断,让它重新工作是痛苦的

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


否则,像大多数人一样执行此操作:忘记设计器。

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

现在,假设设置了设计时viewmodel,可以事先检查所有值是否都是布尔值:

if(values.All(v => v is bool))
{
   //Do regular computation
}
else
{
   //Handle edge case
}
但一旦任何视图变得复杂,设计师就会中断,让它重新工作是痛苦的

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


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

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

if(DesignerProperties.GetIsInDesignMode(Application.MainWindow))
为转换器提供默认值。这将删除错误


VS designer是一个很难对付的野兽,我得出的结论是,它不值得付出努力。但您可以使用:

if(DesignerProperties.GetIsInDesignMode(Application.MainWindow))
为转换器提供默认值。这将删除错误