2011-11-11 3 views
9

저는 제어 라이브러리를 작성하고 있습니다. 이 라이브러리에는 사용자 UIElements로 채워지는 일부 사용자 정의 패널이 있습니다. 나는이 작성하는 경우,첨부 된 속성을 다른 종속성 속성에 바인딩 할 수 없습니다.

// Attached properties common to every UIElement 
public static class MyLibCommonProperties 
{ 
    public static readonly DependencyProperty TitleProperty = 
     DependencyProperty.RegisterAttached( 
      "Title", 
      typeof(String), 
      typeof(UIElement), 
      new FrameworkPropertyMetadata(
       "NoTitle", new PropertyChangedCallback(OnTitleChanged)) 
      ); 

    public static string GetTitle(UIElement _target) 
    { 
     return (string)_target.GetValue(TitleProperty); 
    } 

    public static void SetTitle(UIElement _target, string _value) 
    { 
     _target.SetValue(TitleProperty, _value); 
    } 

    private static void OnTitleChanged(DependencyObject _d, DependencyPropertyChangedEventArgs _e) 
    { 
     ... 
    } 
} 

다음 : 내 lib 디렉토리의 모든 자식 요소는 "제목"속성이 있어야하기 때문에, 나는 다음을 썼다

<dl:HorizontalShelf> 
    <Label dl:MyLibCommonProperties.Title="CustomTitle">1</Label> 
    <Label>1</Label> 
    <Label>2</Label> 
    <Label>3</Label> 
</dl:HorizontalShelf> 

모든 것이 잘 작동하고 속성이 지정됩니다 값하지만이 같은 다른 UIElement에 DependencyProperty에 해당 속성을 결합하려고하면 :

<dl:HorizontalShelf> 
    <Label dl:MyLibCommonProperties.Title="{Binding ElementName=NamedLabel, Path=Name}">1</Label> 
    <Label>1</Label> 
    <Label>2</Label> 
    <Label Name="NamedLabel">3</Label> 
</dl:HorizontalShelf> 

예외가 발생 될 것이다 : "A 'setTitle이라는'속성을 설정할 수 없습니다 '바인딩' '라벨'유형의 'Binding'은 DependencyObject의 DependencyProperty에만 설정할 수 있습니다. "

"Name "에 바인딩하는 대신 MyLibCommonProperties에 정의 된 다른 연결된 속성에 바인딩하면 바인딩이 제대로 작동하는 것 같습니다. 사전에

감사합니다.

+0

안녕, MyLibCommonProperties는 DependecyObject –

+1

그냥 추측에서 파생해야하지만, DependencyObject에로 가져 오기/setTitle이라는 첫 번째 매개 변수를하지 변경할 수 UIElement에 . 또한 Attache 속성을 등록하는 동안 세 번째 매개 변수는 원하는 속성이 아니라 첨부 된 속성의 소유자 여야합니다. MyLibCommonProperties로 변경하십시오. – dowhilefor

+0

'HorizontalShelf '란 무엇입니까? StackPanel 또는 이와 유사한 빌트인 컨트롤에서 대신 사용해 보셨습니까? 다른 모든 것은 괜찮아 보입니다. HorizontalShelf는 자식을 논리적 인 자식으로 인식하지 못하는 사용자 지정 컨트롤이라고 가정 할 수 있습니다. 여기를 참조하십시오 : http://kentb.blogspot.com/2008/10/customizing-logical-children.html –

답변

13

내가 바인딩은 암시 적으로 부모 클래스 사양을 사용하기 때문에이있을 것 같아요 MyLibCommonProperties

public static readonly DependencyProperty TitleProperty = 
    DependencyProperty.RegisterAttached( 
     "Title", 
     typeof(String), 
     typeof(MyLibCommonProperties), // Change this line 
     new FrameworkPropertyMetadata(
      "NoTitle", new PropertyChangedCallback(OnTitleChanged)) 
     ); 

에 DependencyProperty에 정의에서 UIElement 교체 전화 했으므로 SetTitle()으로 전화하여 MyLibCommonProperties.SetTitle() 대신 MyLibCommonProperties.SetTitle()을 호출합니다.

사용자 지정 TextBox 속성에서 동일한 문제가있었습니다. 내가 typeof(TextBox)를 사용하는 경우, 나는 값에 바인딩 할 수 있지만 그때 typeof(TextBoxHelpers)를 사용한 경우 나는

+1

+1 환상적! 감사합니다 – Trap

+0

나를 위해 너무 작동합니다. 감사 – IFink

관련 문제