1

DataContext 종속성 속성에 할당 된 경우에만 작동하는 것으로 간주되는 태그 확장에서 작업하고 있습니다. TargetProperty가 Markup Extension 내의 DataContext 속성인지 확인하는 방법

내가 더 "안전한"방법을 싶습니다 .. 이제

public abstract class DataContextAssignableExtensionBase : MarkupExtension 
{ 

    private void ThrowOnUnsupportedProperty(IServiceProvider serviceProvider) 
    { 
     var dataContextProp = TargetProperty as DependencyProperty; 

     if (dataContextProp == null) 
      throw new ... 
     if (!(dataContextProp.Name.Equals("DataContext") || dataContextProp.Name.Equals("RuntimeDataContext"))) 
      throw new ... 
    } 
} 

, DataContext에 가장 아마 영원히의 DataContext라는 이름의,하지만 여전히 유지됩니다 :

는 다음과 같은 방식으로 확장하여 적용됩니다 확장이 DataContext 종속성 속성에 할당되었는지 확인합니다.

도움을 많이 주시면 감사하겠습니다.

답변

1

그냥 속성 정의에 대한 참조 비교를 수행합니다

if (dataContextProp != FrameworkElement.DataContextProperty) 
    throw ... 
0

이 @MikeStrobel 한 말에 추가를, 유일한 방법은 지정된 DependencyProperty에 디자인 및 런타임 모두에서의 DataContext 속성에 있는지 여부를 알고 .NET 4.5과 VS2013은 다음과 같습니다

if((dependencyProperty == FrameworkElement.DataContextProperty 
|| dependencyProperty == FrameworkContentElement.DataContextProperty) 
|| (DesignerProperties.GetIsInDesignMode(dependencyObject)&& 
dataContextProp.Name.Equals("RuntimeDataContext")))) 
{ 

} 
하는 DependencyProperty 및 DependencyObject에이 참조가 어떻게 든 얻을 수있다

,

그리고 dependen cyProperty는 dependencyObject 참조에 대한 컨텍스트에서 가져 왔습니다.

관련 문제