2009-12-11 11 views
15

Mam kontrolę w WPF, który ma unikalny Uid. Jak mogę pobrać obiekt przez jego Uid?Uzyskaj obiekt według jego Uid w WPF

+1

Proszę opracować. Jaki jest twój UID? Jak to jest ustawione? –

+0

Jest to właściwość zależności jakiejkolwiek kontroli w wpf lub silverlight .. udało mi się rozwiązać to, ale zastanawiałem się, czy istnieje wbudowana metoda. – jose

Odpowiedz

11

Prawie musisz to zrobić brutalną siłą. Oto metoda rozszerzenie pomocnika można użyć:

private static UIElement FindUid(this DependencyObject parent, string uid) 
{ 
    var count = VisualTreeHelper.GetChildrenCount(parent); 
    if (count == 0) return null; 

    for (int i = 0; i < count; i++) 
    { 
     var el = VisualTreeHelper.GetChild(parent, i) as UIElement; 
     if (el == null) continue; 

     if (el.Uid == uid) return el; 

     el = el.FindUid(uid); 
     if (el != null) return el; 
    } 
    return null; 
} 

Następnie można nazwać tak:

var el = FindUid("someUid"); 
+0

Czy "GetChild (rodzic, N)" nie ma złożoności O (N)? Metoda foreach wydaje mi się czystsza (i jaśniejsza). – AgentFire

2

Jest lepiej.

public static UIElement FindUid(this DependencyObject parent, string uid) { 
    int count = VisualTreeHelper.GetChildrenCount(parent); 

    for (int i = 0; i < count; i++) { 
     UIElement el = VisualTreeHelper.GetChild(parent, i) as UIElement; 
     if (el != null) { 
      if (el.Uid == uid) { return el; } 
      el = el.FindUid(uid); 
     } 
    } 
     return null; 
} 
+0

Nie może być lepiej, jeśli twój kod nie działa. Twoja rekursja jest zepsuta. Wynik "el.FindUid (uid)", jeśli nie jest zerowy, musi zostać zwrócony. –

2
public static UIElement GetByUid(DependencyObject rootElement, string uid) 
{ 
    foreach (UIElement element in LogicalTreeHelper.GetChildren(rootElement).OfType<UIElement>()) 
    { 
     if (element.Uid == uid) 
      return element; 
     UIElement resultChildren = GetByUid(element, uid); 
     if (resultChildren != null) 
      return resultChildren; 
    } 
    return null; 
} 
1

Problem miałem z góry odpowiedź jest taka, że ​​to nie będzie wyglądać wewnątrz kontroli treści (takich jak formanty użytkownika) dla elementów należących do ich treści. W celu przeszukania w nich rozszerzyłem funkcję, aby spojrzeć na właściwość Zawartość zgodnych kontrolek.

public static UIElement FindUid(this DependencyObject parent, string uid) 
{ 
    var count = VisualTreeHelper.GetChildrenCount(parent); 

    for (int i = 0; i < count; i++) 
    { 
     var el = VisualTreeHelper.GetChild(parent, i) as UIElement; 
     if (el == null) continue; 

     if (el.Uid == uid) return el; 

     el = el.FindUid(uid); 
     if (el != null) return el; 
    } 

    if (parent is ContentControl) 
    { 
     UIElement content = (parent as ContentControl).Content as UIElement; 
     if (content != null) 
     { 
      if (content.Uid == uid) return content; 

      var el = content.FindUid(uid); 
      if (el != null) return el; 
     } 
    } 
    return null; 
} 
Powiązane problemy