2012-11-10 2 views
2

나는 아주 단순 연결된 속성 만든 :건물 + 스타일 첨부> 경우 ArgumentNullException

: XAML에서이 완벽하게 잘 작동 설정

public static class ToolBarEx 
{ 
    public static readonly DependencyProperty FocusedExProperty = 
     DependencyProperty.RegisterAttached(
      "FocusedEx", typeof(bool?), typeof(FrameworkElement), 
      new FrameworkPropertyMetadata(false, FocusedExChanged)); 

    private static void FocusedExChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     if (d is ToolBar) 
     { 
      if (e.NewValue is bool) 
      { 
       if ((bool)e.NewValue) 
       { 
        (d as ToolBar).Focus(); 
       } 
      } 
     } 
    } 

    public static bool? GetFocusedEx(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(FocusedExProperty); 
    } 

    public static void SetFocusedEx(DependencyObject obj, bool? value) 
    { 
     obj.SetValue(FocusedExProperty, value); 
    } 
} 

을, 그러나 나는 스타일 내에서 설정을 시도하는 경우

런타임 중에 ArguemntNullException을 수신합니다 ("값은 null 일 수 없습니다. 매개 변수 이름 :.. 재산 ") 내가 여기에 무엇이 잘못되었는지 알아낼 수 없습니다

는 어떤 힌트 appriciated됩니다

+0

예외가 발생하면 스택 추적을보십시오. 그러면 코드가 실패한 위치를 찾을 수 있습니다. 그래도 문제를 해결할 수 없다면 스택 트레이스를 질문과 함께 게시하십시오. –

답변

6

부착 종속성 속성을 등록하는 것은 잘못 ownerType 인수를 지정할 때 만든 일반적인 실수는이 항상해야합니다!. ToolBarEx 여기에 등록 클래스 :

public static readonly DependencyProperty FocusedExProperty = 
    DependencyProperty.RegisterAttached(
     "FocusedEx", typeof(bool?), typeof(ToolBarEx), 
     new FrameworkPropertyMetadata(false, FocusedExChanged)); 

그리고 당신이 안전하게 boolNewValue 캐스팅 할 수있는 속성을 변경 핸들러에서 불필요한 코드를 피하기위한 :

private static void FocusedExChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    var toolBar = d as ToolBar; 
    if (toolBar != null && (bool)e.NewValue) 
    { 
     toolBar.Focus(); 
    } 
} 
관련 문제