IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Parcours d'un arbre XAML


Sujet :

Windows Presentation Foundation

  1. #1
    Membre à l'essai
    Inscrit en
    Août 2006
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 39
    Points : 20
    Points
    20
    Par défaut Parcours d'un arbre XAML
    Bonjour,

    J'essai de parcourir un arbre XAML en profondeur afin d'associer à chaque élément trouvé une action spécifique: si le noeud est un bouton, associer actionBouton, si le noeud est un textField, associer actionTextfield, etc...

    Le problème est que mon parcours ne fonctionne pas du tout...

    Ma Méthode qui me permet de charger le fichier XAML et de créer l'arbre associé. Je pars du noeud racine et parcours recursivement chaque enfant. Pour le moment je n'affiche que le type du noeud en cours:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    public void LoadXAMLMethod()
            {
                try
                {
                    StreamReader mysr = new StreamReader("Page1.xaml");
                    DependencyObject rootObject = XamlReader.Load(mysr.BaseStream) as DependencyObject;
     
                    System.Collections.IEnumerable RootChildren = null;
     
                    RootChildren = LogicalTreeHelper.GetChildren(rootObject);
                    foreach (DependencyObject obj in RootChildren)
                    {
                        System.Diagnostics.Debug.WriteLine(obj.DependencyObjectType.Name.ToString());
                        BrowseTree(obj, RootChildren, 0);
                    }
                    this.Content = rootObject;
                }
                catch (FileNotFoundException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
    Et voici ma méthode de parcours qui affiche le type du noeud en cours:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    public void BrowseTree(DependencyObject RootObject, System.Collections.IEnumerable RootChildren, int niveau)
            {
                RootChildren = LogicalTreeHelper.GetChildren(RootObject);
     
                try
                {
                    System.Diagnostics.Debug.WriteLine("niveau: " + niveau);
                    foreach (DependencyObject obj in RootChildren)
                    {
                        System.Diagnostics.Debug.WriteLine(obj.DependencyObjectType.Name.ToString());
                        niveau = niveau + 1;
                        BrowseTree(obj, RootChildren, niveau);
                        return;
                    }
                    return;
                }
                catch (InvalidCastException e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                    return;
                }
           }
    Le fichier XAML de test:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
     
    <Page 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
      >
      <Grid>
        <Grid.Background>
          <LinearGradientBrush StartPoint="0,0" EndPoint="0.2,1">
            <GradientStop Color="#FF0000" Offset="0" />
            <GradientStop Color="#FFFFFF" Offset="1.0" />
          </LinearGradientBrush>
        </Grid.Background>
        <TabControl TabStripPlacement="Top" Margin="15, 15, 15, 15">
          <TabItem Name="Tab1" Header="Page 1">
            <TabItem.Content>
              <Canvas>
                <Border Canvas.Top="5" Canvas.Left="5" BorderBrush="Black" BorderThickness="2">
                  <WrapPanel Name="XamlWP" HorizontalAlignment="Left" Width="100" Background="#FFFFFF">
                    <Label>
                      LC Module :
                    </Label>
                    <TextBox 
                      BorderThickness="1"
                      Name="TextBox2" Width="100" Height="20"
                      >
                    </TextBox>
                    <TextBox  
                      BorderThickness="1"
                      Name="TextBox3" Width="100" Height="20">
                    </TextBox>
                  </WrapPanel>
                </Border>
                <Button 
                  Canvas.Top="100" Canvas.Left="15" 
                  Name="SaveButton" Width="60" Height="20">
                  Save
                </Button>
              </Canvas>
            </TabItem.Content>
          </TabItem>
          <TabItem Name="Tab2" Header="Page 2">
          </TabItem>
     
        </TabControl>
      </Grid>
    </Page>
    En sortie j'obtiens:

    Grid
    niveau: 0
    TabControl
    niveau: 1
    TabItem
    niveau: 2
    Une exception de première chance de type 'System.InvalidCastException' s'est produite dans LoadXAMLdynamically.exe
    Unable to cast object of type 'System.String' to type 'System.Windows.DependencyObject'.


    Le "DependencyObject obj" de ma méthode BrowseTree est null au moment de l'exception.

    Il doit probablement s'agir de quelque de stupide, mais je ne vois pas du tout :S

    Cordialement,
    Sacoum.

  2. #2
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Points : 19 434
    Points
    19 434
    Par défaut
    Dans ta méthode BrowseTree, fais plutôt une boucle do... while:

    tant que LogicalTreeHelper.GetChildren ne te renvoit pas null, accède à LogicalTreeHelper.GetChildren et affiche le

  3. #3
    Membre à l'essai
    Inscrit en
    Août 2006
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 39
    Points : 20
    Points
    20
    Par défaut
    Merci pour ta réponse Thomas, mais qu'entends tu par "accède à LogicalTreeHelper.GetChildren" ?

  4. #4
    Membre à l'essai
    Inscrit en
    Août 2006
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 39
    Points : 20
    Points
    20
    Par défaut
    Je ne sais pas si il s'agit de la meilleure solution mais cela fonctionne. Voici donc la nouvelle méthode BrowseTree:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
            public void BrowseTree(DependencyObject RootObject, System.Collections.IEnumerable RootChildren, int niveau)
            {
                RootChildren = LogicalTreeHelper.GetChildren(RootObject);
     
                try
                {
                    do
                    {
                        niveau = niveau + 1;
                        System.Collections.IEnumerator enumChild = RootChildren.GetEnumerator();
                        if (enumChild.MoveNext())
                        {
                            RootObject = enumChild.Current as DependencyObject;
                            if (RootObject != null)
                            {
                                System.Diagnostics.Debug.WriteLine(RootObject.GetType().ToString());
                                BrowseTree(RootObject, RootChildren, niveau);
                            }
                        }
                        else
                        {
                            RootChildren = null;
                        }
                    }
                    while (RootChildren != null);
                    return;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                    return;
                }
            }
    Qu'en pensez vous ?

  5. #5
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Points : 19 434
    Points
    19 434
    Par défaut
    Que c'est pas mal du tout et que cela correspond à ce que je te disais

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Pb parcour d'un arbre.
    Par ptipoutche dans le forum Algorithmes et structures de données
    Réponses: 6
    Dernier message: 08/11/2006, 17h15
  2. Réponses: 4
    Dernier message: 19/02/2006, 18h43
  3. [debutant] parcours en profondeur arbre n-aire
    Par tx dans le forum Langage
    Réponses: 1
    Dernier message: 15/02/2006, 03h56
  4. parcours d'un arbre en sql
    Par dor_boucle dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 02/02/2006, 11h10
  5. Ordre de parcours de l'arbre...
    Par Sylvain James dans le forum XML/XSL et SOAP
    Réponses: 3
    Dernier message: 01/12/2002, 18h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo