2010-02-26 32 views
0

嗨,我有ContentControl,我正在應用我有ListBox的樣式。我想在Xaml.cs中找到ListBoxItem。如何找到在silverlight 4中添加風格的子控件?

+0

您是在尋找ListBoxItem模板,還是想要訪問ListBox中的單個項目? – Gabe 2010-02-26 06:56:33

+0

我想在列表框中使用不透明的項目。 – jolly 2010-02-26 06:59:35

回答

1

我發現了一個解決方案。我已經實現了一個方法來查找需要兩個參數的可視元素,父元素和控件的類型需要查找。在調用此方法之前,我使用了ApplyTemplate方法

public static FrameworkElement[] FindDownInTree(FrameworkElement parent, Type controlType) 
{ 
    List<FrameworkElement> lst = new List<FrameworkElement>(); 

    FindDownInTree(lst, parent, controlType); 

    if (lst.Count > 0) 
     return lst.ToArray(); 

    return null; 
} 



private static void FindDownInTree(List<FrameworkElement> lstElem, DependencyObject parent, Type controlType) 
{ 

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) 
    { 
     DependencyObject visual = VisualTreeHelper.GetChild(parent, i); 

     if (controlType.IsInstanceOfType(visual)) 
     { 
      lstElem.Add(visual as FrameworkElement); 
     } 

     if (visual != null) 
     { 
      FindDownInTree(lstElem, visual, controlType); 
     } 

    } 
} 
+0

C&P ...接受一些答案ms .. :) – Malcolm 2010-11-04 12:56:00

相關問題