2009-06-25 8 views
1

ContentProperty를 "텍스트"로 설정하는 데 문제가 있습니다. 내가받은 오류는 다음과 같습니다.'Text'속성의 ContentPropertyAttribute가 올바르지 않습니다.

'MyType'유형의 유효하지 않은 ContentPropertyAttribute가 'Text'속성을 찾을 수 없습니다.

코드 뒤에 다음과 같습니다

실제로 내가하는 DependencyProperty 이외의 CLR 속성 뭔가 이름을 경우 작동하도록 가지고있다
[ContentProperty("Text")] 
    public partial class MyType: UserControl 
    { 
     public MyType() 
     { 
      InitializeComponent(); 
     } 

     public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", 
                          typeof (string), 
                          typeof(MyType))); 

     public static string GetText(DependencyObject d) 
     { 
      return (string) d.GetValue(TextProperty); 
     } 

     public static void SetText(DependencyObject d, string value) 
     { 
      d.SetValue(TextProperty, value); 
     } 


     public string Text 
     { 
      get 
      { 
       return (string)GetValue(TextProperty); 
      } 
      set 
      { 
       SetValue(TextProperty, value); 
      } 
     } 
    } 

- 잘못 내가 사용하고 DependencyProperties?

답변

4

typeof (LinkText)가 typeof (MyType) 여야한다고 생각했지만 컴파일 할 테스트 프로젝트를 가져올 수있었습니다. 오류의 원인이되는 XAML 파일을 게시 할 수 있습니까?

편집 : 후속 조사

귀하의 문제는 코드 샘플에서 당신이 가지고있는 두 개의 정적 방법이다. 그것들을 제거 해보면 컴파일되고 작동해야합니다. 정적 메서드는 종속 속성이 아닌 첨부 속성에서만 작동합니다. TextProperty 유형 문자열입니다

... new PropertyMetadata(false) ... 

때문에, 기본 값에 대한 문자열을 기대 :

+0

죄송합니다. 그저 쉽게 따라 할 수 있도록 형식 이름을 정리했습니다. typeof (MyType)라고 가정합니다. –

+1

"새 PropertyMetadata (false))"을 변경해야합니다. "new PropertyMetadata (null))"와 같은 문자열 값으로 변경합니다. – micahtan

+0

실제로 GetText와 SetText를 제거하면 오류가 제거됩니다. – rmoore

1

오류가 당신에게서오고는에 설정된 기본 값을입니다. 시도해보십시오.

public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register(
     "Text", 
     typeof (string), 
     typeof(MyType), 
     new PropertyMetadata(String.Empty)); 
+0

고마워, 내가 가진 바보 같은 오류를 설명하지만 ContentProperty ("Text")가 실패하지 않는 이유는 설명하지 않는다. –

관련 문제