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 指定的强制转换无效。在 myassemblyname.MultiEnabledToEnabled.Convert(Object[] values, Type targetType, Object parameter, CultureInfoculture) in C:...\Converters.cs:line 176 at System.Windows.Data.MultiBindingExpression.TransferValue() at System.Windows .Data.MultiBindingExpression.Transfer() 在 System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings) 在 System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance) 在 System.Windows.Data.MultiBindingExpression.MS.Internal.Data .IDataBindEngineClient.AttachToContext(Boolean lastChance) 在 MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) 在 MS.Internal.Data.DataBindEngine.Run(Object arg) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate回调,对象参数,