2012-10-01 4 views
1

Visual Studio 2010 디자이너는 처리되지 않은 예외가 MultiValueConverter에서 발생했지만 프로그램을 빌드 할 수 있으며 잘 작동합니다 (다중 연결도 가능함).WPF MultiBinding VS 디자이너 예외

  <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 지정한 캐스트

enter image description here

XAML은 (내가 생성자 window.DataContext 설정) 유효하지 않습니다. at myassemblyname.MultiEnabledToEnabled.Convert (Object [] values, Type targetType, Object 매개 변수, CultureInfo 문화권) C : ... \ Converters.cs : Line 176 at System.Windows.Data.MultiBindingExpression.TransferValue() at System.Windows.Data.MultiBindingExpression에서 System.Windows.Data.MultiBindingExpression.AttachToContext (부울 lastChance) 에서 System.Windows.Data.MultiBindingExpression.UpdateTarget (부울 includeInnerBindings) 에서 System.Windows.Data.MultiBindingExpression.Transfer() .MS.Internal.Data.IDataBindEngineClient.AttachToContext (Boolean lastChance) MS.Internal.Data.DataBindEngine.Task.Run (부울 lastChance) at MS.Internal.Data.DataBindEngine.Run (Object arg) at System. Windows.Threading.Excep tionWrapper.InternalRealCall MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen에서 (위임 콜백, 객체 인수, INT32 numArgs) (오브젝트 소스, 위임 방법, 인수 객체, INT32 numArgs, 위임 catchHandler)

답변

2

내 추측은 그 바인딩은 초기화 이전에 발생하며 오브젝트 컬렉션의 적어도 하나의 값은 DependencyProperty.UnsetValue이며 캐스트가 유효하지 않게됩니다.

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

그러나 곧 모든보기가 복잡해진다으로, 디자이너 중단하고 모든 값이 참 부울 경우

이제, 사용자가 설정 뷰 모델 설계 시간을 가정, 당신은 사전에 확인할 수 있습니다 다시 작동하게하는 것은 고통 스럽습니다.

Expression Blend는 디자이너를 절대적으로 원하지만 디자인 타임 환경을 설정하는 데 신경 쓰지 않는 경우이 기능을보다 잘 처리합니다.

그렇지 않으면 대부분의 사람들처럼 : 디자이너에 대해 잊어 버려야합니다.

2

디자이너 VS는 사용하기에 힘든 짐승이며 노력할만한 가치가 없다고 결론을 내 렸습니다. 하지만 다음을 사용할 수 있습니다 :

if(DesignerProperties.GetIsInDesignMode(Application.MainWindow)) 

변환기의 기본값을 제공합니다. 이렇게하면 오류가 제거됩니다.

DesignerProperties.GetIsInDesignMode method at msdn