mai
2006
Si vous travaillez avec Windows Presentation Foundation (WPF, anciennement connu sous le nom de code: Avalon), vous pouvez être amené à avoir besoin de travailler/connaitre tout à propos de l’arbre de graph qui composent vos contrôles.
Pour cela, j’ai développé une petite classe, nommée UtilityWPF, et disponible ici: http://morpheus.developpez.com/trucs/classes/
Pour ceux qui ne veulent pas aller voir ce que cela donne, voici directement le code source de cette classe:
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;
namespace UtilityWPF
{
class Utility
{
private static StringBuilder sbListControls;
public static StringBuilder GetVisualTreeInfo(Visual element)
{
if (element == null)
{
throw new ArgumentNullException(String.Format("Element {0} is null !", element.ToString()));
}
sbListControls = new StringBuilder();
GetControlsList(element, 0);
return sbListControls;
}
private static void GetControlsList(Visual control, int level)
{
const int indent = 4;
int ChildNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i <= ChildNumber - 1; i++)
{
Visual v = VisualTreeHelper.GetChild(control, i);
sbListControls.Append(new string(' ', level * indent));
sbListControls.Append(v.GetType());
sbListControls.Append(Environment.NewLine);
if (VisualTreeHelper.GetChildrenCount(v) > 0)
{
GetControlsList(v, level + 1);
}
}
}
}
}
Son utilisation est relativement simple:
StringBuilder sb = Utility.GetVisualTreeInfo(button);
string[] controls = sb.ToString().Split(Environment.NewLine.ToCharArray());
Le résultat, suivant le contrôle que vous observez, peut donner quelque chose comme ceci:
AvalonApplication1.Window1
System.Windows.Controls.Border
System.Windows.Controls.Grid
System.Windows.Documents.AdornerDecorator
System.Windows.Controls.ContentPresenter
System.Windows.Controls.Grid
System.Windows.Controls.Button
System.Windows.Controls.Grid
System.Windows.Shapes.Rectangle
System.Windows.Shapes.Rectangle
System.Windows.Shapes.Rectangle
System.Windows.Shapes.Rectangle
System.Windows.Shapes.Rectangle
System.Windows.Shapes.Rectangle
System.Windows.Shapes.Rectangle
System.Windows.Shapes.Rectangle
System.Windows.Controls.Border
System.Windows.Controls.FlowPanel
System.Windows.Controls.ContentPresenter
System.Windows.Controls.Text
System.Windows.Controls.Grid + GridLinesRenderer
System.Windows.Controls.Grid + GridLinesRenderer
System.Windows.Documents.AdornerLayer
System.Windows.Controls.Primitives.StatusBar
System.Windows.Controls.Primitives.ResizeGrip
System.Windows.Controls.Grid+GridLinesRenderer
En espérant que cela vous sois utile
A+
Articles récents
- [Mix 07] Annonces diverses dont la possibilité de développer des applications SilverLight avec du code managé !
- [WPF] Article d’introduction à Windows Presentation Foundation Everywhere
- [WPF] Les CTP de WPF /E et Expression Studio sont en ligne !
- [Event] Première rencontre du Dotnet User Group (DUG)
- [.NET 3] Présentation du Framework .NET 3.0
Commentaires récents
- [Tips] Ouvrir un fichier vhd sans lancer une VPC dans
- [WPF] Article d’introduction à Windows Presentation Foundation Everywhere dans
- [WPF] Article d’introduction à Windows Presentation Foundation Everywhere dans
- Visual Basic Express 2005 et Visual Web Developer Express 2005 disponibles EN FRANCAIS ! dans
- [.NET 3] Présentation du Framework .NET 3.0 dans
Archives
- avril 2007
- décembre 2006
- novembre 2006
- octobre 2006
- septembre 2006
- août 2006
- juillet 2006
- juin 2006
- mai 2006
- avril 2006
- mars 2006
- février 2006
- janvier 2006
- décembre 2005
- novembre 2005
- octobre 2005
- septembre 2005
- août 2005
- juillet 2005
- juin 2005
- mai 2005
- avril 2005
- mars 2005
- février 2005
- janvier 2005
- décembre 2004
- novembre 2004
- octobre 2004