2009-12-11 21 views
15

'dan yararlanın Benzersiz bir UID'si olan WPF'de bir kontrolüm var. Nesneyi Uid tarafından nasıl geri alabilirim?Nesnesi WPF

+1

hakkında detaylı bilgi veriniz. UID'niz nedir? Nasıl kurulur? –

+0

Bu wpf veya silverlight herhangi bir denetimin bir bağımlılık özelliği .. Ben bunu çözmek başardı, ama yerleşik bir yöntem varsa merak ediyordum. – jose

cevap

11

Hemen hemen kaba güç kullanarak yapmak zorundayız. Burada kullanabileceğiniz bir yardımcı uzantısı yöntemidir:

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; 
} 

Sonra böyle diyebilirsin:

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

GetChild (ana, N) 'O (N) karmaşıklığına sahip değil mi? Foreach yaklaşımı bana daha temiz (ve daha net) görünüyor. – AgentFire

2

Bu daha iyidir.

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

Kodunuz çalışmıyorsa daha iyi olamaz. Sizin özyineniz bozuldu. El.FindUid (uid) 'nin sonucu, null değilse, iade edilmelidir. –

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

Üst cevap vardı Bir mesele içeriklerinde elemanları (örneğin, kullanıcı kontrolleri gibi) içerik denetimleri içine bakmak değil olmasıdır. Bunların içinde arama yapmak için, uyumlu kontrollerin İçerik özelliğine bakmak için fonksiyonu genişlettim.

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; 
}