Mam kontrolę w WPF, który ma unikalny Uid. Jak mogę pobrać obiekt przez jego Uid?Uzyskaj obiekt według jego Uid w WPF
Odpowiedz
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");
Czy "GetChild (rodzic, N)" nie ma złożoności O (N)? Metoda foreach wydaje mi się czystsza (i jaśniejsza). – AgentFire
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;
}
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. –
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;
}
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;
}
- 1. Uzyskaj indeks kolumny według nagłówka kolumny w WPF DataGrid
- 2. Uzyskaj obiekt nadrzędny TreeViewItem w HierarchicalDataTemplate w pakiecie WPF
- 3. Uzyskaj nazwę projektu WPF
- 4. WPF Uzyskaj UserControl właściciela
- 5. Pobierz obiekt według id()?
- 6. Uzyskaj kluczową wartość słownika według indeksu w jQuery
- 7. Uzyskaj wartość pozycji listbox według indeksu
- 8. AngularJS - Uzyskaj dane według ID
- 9. Uzyskaj funkcję według nazwy dynamicznie w Kotlin
- 10. Jak przekazać obiekt według wartości?
- 11. Podziel obiekt xts według zdarzeń
- 12. Uzyskaj tag wybranego elementu w WPF ComboBox
- 13. Hibernacja: pobierz obiekt według identyfikatora
- 14. Pobierz obiekt według właściwości w Core Data
- 15. Uzyskaj obiekt JSON przez atrybut
- 16. Uzyskaj bieżący ograniczony obiekt w ItemTemplate ListView
- 17. sortowanie wykresu według jego ciężaru krawędzi. pyton
- 18. XPath: Jak wybrać węzeł według jego atrybutu?
- 19. Jak przekonwertować obiekt WriteableBitmap na obiekt BitmapImage w WPF
- 20. Jak znaleźć obiekt w tablicy ArrayList według właściwości
- 21. Zmiana zabezpieczeń Android UID
- 22. Python - obiekt może być jego własnym typem?
- 23. AS3 Obróć obiekt wokół jego punktu środkowego.
- 24. Uzyskaj ciąg znaków według indeksu - Java
- 25. Uzyskaj właściwość klasy PHP według ciągu znaków
- 26. Android Uzyskaj grupę kontaktów według ID
- 27. Jak przesłać obiekt do jego rzeczywistego typu?
- 28. Uzyskaj atrybut obiektu ActiveRecord według ciągu znaków
- 29. Jak znaleźć element w Watinie według jego nazwy?
- 30. Jak zdobyć dzieci kontenera WPF według typu?
Proszę opracować. Jaki jest twój UID? Jak to jest ustawione? –
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