【发布时间】: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