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指定的强制转换无效 . at myassemblyname.MultiEnabledToEnabled.Convert(Object [] values,Type targetType,Object parameter,CultureInfo culture)C:... \ Converters.cs:第176行,System.Windows.Data.MultiBindingExpression.TransferValue(),位于System.Windows位于System.Windows.Data.MultiBindingExpression.MS.Internal.Data的System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance)的System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings)中的.Data.MultiBindingExpression.Transfer()位于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) ms.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)中的回调,Object args,Int32 numArgs)
2 回答
我最好的猜测是绑定在一些初始化之前发生,并且对象集合中的至少一个值是
DependencyProperty.UnsetValue
,使得转换无效 .现在,假设您已经设置了设计时视图模型,您可以事先检查所有值是否确实是布尔值:
但是一旦任何观点变得复杂,设计师就会崩溃,让它再次运作是痛苦的 .
Expression Blend可以更好地处理这个问题,如果你绝对想要一个设计师但是不能打扰设置一个设计时环境,那就去做吧 .
否则就像大多数人一样:忘记设计师 .
VS设计师是一个很难与之合作的人,我得出的结论是,这不值得付出努力 . 但你可以使用:
为转换器提供默认值 . 这将删除错误 .
DesignerProperties.GetIsInDesignMode method at msdn