2012-01-18 4 views
0

반사 GetBindingExpression 대상 얻는 방법 :나는이 시점에서 알아낼 수없는 무언가에 약간의 도움이 필요 할 수

내가 특정에 (RESX-파일을 다양한에 클래스를 통해) XAML - 바인딩을 업데이트 할 필요를 특정 이름 접두사가있는 컨트롤의 이벤트입니다. 컨트롤이 서로 다른 유형이와 내가 같은 페이지가 미래에 somewhen를 보는 방법을 더 알고를 할 수 없기 때문에, 나는

var meth1 = control.GetType().GetMethod("GetBindingExpression"); 
var meth2 = control.GetType().GetMethod("SetBinding"); 
BindingExpression be = (BindingExpression)meth1.Invoke(target, null); 
Binding bind = be.ParentBinding; 
meth2.Invoke(target, new object[] { bind }); 

가 보인다

뭔가

처럼 ... 단지 반사와 함께 그렇게하고 싶습니다 ...

내가 여기보다는 쉽게 뭔가를 놓친 거지 확신

... 나에게 올바른 생각,하지만 난 전에 DependencyObject의 유형을 모른 채 DependencyObject에서 대상으로하는 DependencyProperty를 얻을 방법을 알아낼 수 없습니다

[편집] 컨트롤을 통해 갈 수 있고 그냥 ResourceManager 객체에서 얻은 새 문자열을 입력 할 수 있다는 것을 알고 있습니다. 컨트롤의 Text-Property,하지만 그 경우에는 Text, Header, 모든 속성을 검사해야합니다. 가능한 경우 반사가 더 깨끗한 것으로 보입니다. 내가 생각에 여기에서

foreach (var f in control.GetType().GetFields()) 
    { 
     DependencyProperty dp = f.GetValue(control) as DependencyProperty; 
     if (dp != null) 
     { 
      BindingExpression be = ((FrameworkElement)control).GetBindingExpression(dp); 
      if (be != null) 
      { 
       // stuff here 
      } 
     } 
    } 

: 당신은 단순히 다음과 같은 방법을 사용할 수 있습니다

+0

컨트롤을 FrameworkElement로 캐스팅하고 리플렉션 대신 직접 메서드를 사용할 수없는 이유는 무엇입니까? –

답변

0

, FrameworkElement로 캐스팅에 관한 두 힌트에

FrameworkElement fe = control as FrameworkElement; 
foreach(PropertyDescriptor pd in TypeDescriptor.GetProperties(control)) 
{ 
    FieldInfo field = control.GetType().GetField(pd.Name + "Property"); 
    if(field == null) 
     continue; 
    DependencyProperty dp = field.GetValue(control) as DependencyProperty; 
    if(dp == null) 
     continue; 
    BindingExpression be = control.GetBindingExpression(dp); 
    if(be == null) 
     continue; 

    // do your stuff here 

} 
+0

감사합니다, 이것은 올바른 방향으로 나를 데려갔습니다. Silverlight TypeDescriptor 클래스를 알 수 없으므로 위의 작동하지 않지만 아래로 완료했습니다. – Seb

0

감사합니다, 나는 다시 궤도에 얻을 관리 작업 완료

관련 문제