2012-02-22 3 views
2

코드 읽기 중에이 질문을 발견했습니다. 검색 MSDN 후에도 동일한 문제가 있습니다. DependencyProperty.Register 방법에 대한 DependencyProperty의 RegisterAttached() 및 Register()에서 다른 동작

http://msdn.microsoft.com/en-us/library/ms597501.aspx

, 그것은 같은 코드 예제가 : 내 질문은, '아무튼 RegisterAttached됩니다

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(....); 
public static void SetIsBubbleSource(UIElement element, Boolean value) 
{ 
    element.SetValue(IsBubbleSourceProperty, value); 
} 
public static Boolean GetIsBubbleSource(UIElement element) 
{ 
    return (Boolean)element.GetValue(IsBubbleSourceProperty); 
} 

: RegisterAttached 방법 http://msdn.microsoft.com/en-us/library/ms597496.aspx를 들어

public static readonly DependencyProperty CurrentReadingProperty = DependencyProperty.Register(...); 
public double CurrentReading 
{ 
    get { return (double)GetValue(CurrentReadingProperty); } 
    set { SetValue(CurrentReadingProperty, value); } 
} 

을, 그것과 같은 코드 예제를 가지고 속성 형식을 사용하고 2 개의 정적 함수를 사용하십시오. 왜?

+0

웹에서 "연결된 동작"을 검색하거나 접미사 서비스가있는 클래스에 대한 WPF 설명서를 확인할 때. 이것에는 몇 가지 추가 이점이 있음을 알 수 있습니다. 연결된 속성을 선언하는 클래스는 DepedencyObject에서 파생 될 필요가 없습니다. 사실 모든 정적 메서드이므로 클래스가 완전히 정적 일 수 있습니다. – dowhilefor

답변

3

RegisterAttached과 관련된 정적 메소드는 하나 개의 클래스에 정의 Canvas.Left 같은 attached properties 가입을위한 것이지만 (DependencyObject 유래의) 임의의 다른 클래스의 인스턴스에 설정 될 수 있기 때문이다. 당신은 예를 들어 다음과 같은 코드에서 버튼에 Canvas.Left을 설정할 수 있습니다 당신은 클래스 버튼에 Left 속성을 추가 할 수 없기 때문에

Canvas.SetLeft(button, 100); 

당신은 정적 인 방법이 필요합니다.