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 :

TreeViewItems depuis des fichiers textes


Sujet :

Windows Presentation Foundation

  1. #1
    Membre habitué Avatar de mailbox
    Profil pro
    Inscrit en
    Février 2010
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Février 2010
    Messages : 140
    Points : 159
    Points
    159
    Par défaut TreeViewItems depuis des fichiers textes
    Bonjour,

    J'ai un treeview personnalisé auquel j'ajoutais des items "en dur" :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
                l = new Indice("CAC40");
                Add(l);
                d = new SecteurActivite("AeroSpatiale");
                l.SecteurActivites.Add(d);
                d.GrandeEntreprises.Add(new GrandeEntreprise("EADS"));
    Pour ne pas tout coder en dur j'ai stocké les nom des "GrandeEntreprises" dans des fichiers .txt, et je crée les nouveaux items du treeview de cette façon :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
                l = new Indice("CAC40");
                Add(l);        
                d = new SecteurActivite("Aerospatiale");
                l.SecteurActivites.Add(d);
                createList("..//..//Resources//cac40//cac40_aerospatiale.txt", d);
    Et voilà la fonction createList :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
            private void createList(string fichier, SecteurActivite d)
            {
                string line;
                System.IO.StreamReader file = new System.IO.StreamReader(@fichier);
     
                while ((line = file.ReadLine()) != null)
                    d.GrandeEntreprises.Add(new GrandeEntreprise(line));
            }
    Pour vous donner une idée de ce treeview :


    Dans les deux cas je peux executer mon programme, seulement depuis que je stock dans les fichiers textes je ne peux plus voir mon appli dans le designer, j'ai cette erreur :


    Si quelqu'un à une idée...merci!

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    Bonjour mailbox....

    Tu specifie un chemin de fichier d'application en relatif en mode design.
    En mode Design le chemin relatif correspond en WPF à VisualStudio\Common\IDE helas ....
    En mode Run-Time(execution) par contre il correspond au chemin d'Application habituel=>\Debug....

    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
       l = new Indice("CAC40");
                Add(l);        
                d = new SecteurActivite("Aerospatiale");
                l.SecteurActivites.Add(d);
                //ligne en cause du chemin relatif 
                createList("..//..//Resources//cac40//cac40_aerospatiale.txt", d);
    Pour avoir ce que tu veux il faut :
    - une variable static pathData au niveau du form pour le chemin de fichier en "dur" (dossier actuel ou sont installes tes fichiers data)...

    -une autre variable private appPath pour le chemin d'application en relatif ...

    Ensuite un bout de code avec un test if:
    -si on est mode design on utilise pathData tel quel (chemin en "dur")
    -si on est en mode run-time on charge dans pathData le chemin relatif store dansappPath...

    code-behind des class ave ta proc createList( transforme en fonction je prefere) et du form avec le test sur les chemins:

    Code c# : 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
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
     
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Collections.ObjectModel;
     
    using System.IO ;
    // ajoutes ces using et une ref a  System.Design
    using System.ComponentModel.Design;
    using System.ComponentModel;
    namespace WpfTreeViewWithListBoxCSharpRevu
    {
        /// <summary>
        /// Logique d'interaction pour Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            //chemin appli à l'execution 
            private string AppPath = Directory.GetCurrentDirectory() + @"\Data\";
     
            //chemin en dur.les 4 fichiers texte hjoint sont un dossier Data du fichier projet...
            public static string pathData = @"C:\Documents and Settings\Bureau\WpfTreeViewWithListBoxCSharpRevu\Data\";
     
            public Window1()
            {
                InitializeComponent();
     
     
                if (!DesignerProperties.GetIsInDesignMode(this))
     
                {
                    /* on n'est pas en design mode */
                    pathData = AppPath;
                }
     
            }
     
     
        }
     
     
        //Grande Entreprise
        public class GrandeEntreprise
        {
            private string _name;
            public string Name { get { return _name; } }
            public GrandeEntreprise(string name)
            {
                _name = name;
            }
        }
     
        //liste Grande Entreprise
        public class GrandeEntrepriseList : ObservableCollection<GrandeEntreprise>
        {
            public GrandeEntrepriseList()
            {
            }
        }
     
        // secteur activite
        public class SecteurActivite
        {
            private string _name;
            public string Name { get { return _name; } }
            private GrandeEntrepriseList _GrandeEntreprises;
            public GrandeEntrepriseList GrandeEntreprises { get { return _GrandeEntreprises; } }
            public SecteurActivite(string name)
            {
                _name = name;
                _GrandeEntreprises = new GrandeEntrepriseList();
            }
        }
     
        // liste secteur activite
        public class SecteurActiviteList : ObservableCollection<SecteurActivite>
        {
            public SecteurActiviteList()
            {
            }
        }
     
        // indice
        public class Indice
        {
            private string _name;
            public string Name { get { return _name; } }
            private SecteurActiviteList _SecteurActivites;
            public SecteurActiviteList SecteurActivites { get { return _SecteurActivites; } }
            public Indice(string name)
            {
                _name = name;
                _SecteurActivites = new SecteurActiviteList();
            }
     
     
        }
     
        // liste indices
        public class ListIndice : ObservableCollection<Indice>
        {
     
     
            public ListIndice( )
            {
                
                Indice l;
                SecteurActivite d;
     
                //indice CAC40
                l = new Indice("CAC40");
                Add(l);
                d = new SecteurActivite("AeroSpatiale");
                l.SecteurActivites.Add(d);
                d = this.createList(Window1.pathData + "CC40_AeroSpatiale.txt", d);
     
                d = new SecteurActivite("Chimie");
                l.SecteurActivites.Add(d);
                d = this.createList(Window1.pathData + "CC40_Chimie.txt", d);
     
                d = new SecteurActivite("Banques");
                l.SecteurActivites.Add(d);
                d = this.createList(Window1.pathData + "CC40_Banques.txt", d);
     
                //  indice SRD
                l = new Indice("SRD");
                Add(l);
                d = new SecteurActivite("SRD-S1");
                l.SecteurActivites.Add(d);
                d = this.createList(Window1.pathData + "SRD-S1_SRD-S11.txt", d);
     
     
                d = new SecteurActivite("SRD-S2");
                l.SecteurActivites.Add(d);
                d = this.createList(Window1.pathData + "SRD-S2_SRD-S22.txt", d);
            }
            public Indice this[string name]
            {
                get
                {
                    foreach (Indice l in this)
                        if (l.Name == name)
                            return l;
     
                    return null;
                }
            }
     
            //tranforme en fonction qui renvoie-d-
            private SecteurActivite  createList(string fichier, SecteurActivite d)
            {
                string line;
                System.IO.StreamReader file = new System.IO.StreamReader(fichier);
     
                while ((line = file.ReadLine()) != null)
                    d.GrandeEntreprises.Add(new GrandeEntreprise(line));
                return d;
            }
     
        }
     
    }
    xaml winform deja vu:
    Code xaml : 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
     
    <Window x:Class="WpfTreeViewWithListBoxCSharpRevu.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTreeViewWithListBoxCSharpRevu"
        Title="Window1" Height="300" Width="300"
        SizeToContent="WidthAndHeight" >
        <Window.Resources >
            <local:ListIndice
                x:Key="myListIndice">
            </local:ListIndice > 
        </Window.Resources>
     
        <Grid  >
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <TreeView
                Grid.Row="0"
                x:Name="treeviewBourse"  
                Background="DarkOrange" 
                BorderBrush="RoyalBlue"
                BorderThickness="4" >
                <TreeViewItem 
                    IsExpanded="True"
                    FontSize="16"
                    Foreground="WhiteSmoke"
                   ItemsSource="{Binding Source={StaticResource myListIndice}}"
                    Header="Indices -de la Deroute -Boursiers 2012" />
            </TreeView>
        </Grid>
    </Window>

    code xaml fichier application:

    Code xaml : 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
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
     
    <Application x:Class="WpfTreeViewWithListBoxCSharpRevu.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTreeViewWithListBoxCSharpRevu"
        StartupUri="Window1.xaml">
        <Application.Resources>
            <local:ListIndice x:Key="MyListIndice"/>
            <!--wrapper le "visual tree" du HierarchicalDataTemplate 
            de chaque  niveau N dans un StackPanel "englobant"-->
     
            <!-- niveau N1(class Indice)-->
            <HierarchicalDataTemplate 
                DataType="{x:Type local:Indice}" >
                <StackPanel>
                    <!--un CheckBox decisif pour les choix au dessus du Header-->
                    <CheckBox 
                            Name="chkHeader"
                            Background="YellowGreen" 
                            Foreground="WhiteSmoke"
                            FontSize="10"
                            FontFamily="Times new  Roman" 
                            FontStyle="Italic"
                            Content="{Binding Path=Name}">
                    </CheckBox>
                    <!--ce niveau N(Class Indice) sera wrappe dans un TreeViewItem  habituel 
                    dont le contenu sera affiche par un ListBox
                    Le listbox pointe son ItemsSource sur l'ItemsSource du 
                    HierarchicalDataTemplate qui est deporte ici-->
                    <TreeViewItem 
                        IsExpanded="True"
                        Foreground="Yellow"
                        FontSize="16"
                        FontWeight="Bold"
                        Background="Red" 
                        Header="{Binding Path=Name}">
                        <ListBox 
                            Name="List1"
                            Background="DarkBlue" 
                            Foreground="WhiteSmoke"
                            ItemsSource="{Binding Path=SecteurActivites}">
                        </ListBox>
                    </TreeViewItem>
                </StackPanel>
            </HierarchicalDataTemplate>
     
            <!--idem pour le niveau N2(class Secteur)-->
            <HierarchicalDataTemplate 
                    DataType="{x:Type local:SecteurActivite}"
                    ItemsSource="{Binding Path=GrandeEntrepriseList}">
                <StackPanel>
                    <CheckBox 
                            Name="chkHeader"
                            Background="YellowGreen" 
                            Foreground="WhiteSmoke"
                            FontSize="10"
                            FontFamily="Times new  Roman" 
                            FontStyle="Italic"
                            Content="{Binding Path=Name}">
                    </CheckBox>
                    <TreeViewItem 
                        IsExpanded="True"
                        Foreground="Yellow"
                        FontSize="16"
                        FontWeight="Bold"
                        Background="Red" 
                        Header="{Binding Path=Name}">
                        <!--ScrollViewer visible vertical pour notre listbox
                        reduire la hauteur du ListBox pour le voir-->
                        <!--idem pour largeur-->
                        <ScrollViewer
                            VerticalScrollBarVisibility="Auto">
                            <ListBox
                                x:Name="List1"
                                ScrollViewer.VerticalScrollBarVisibility="Auto"
                                Width="200"
                                Height="50"
                                Background="YellowGreen" 
                                Foreground="WhiteSmoke"
                                ItemsSource="{Binding Path=GrandeEntreprises}">
                            </ListBox>
                        </ScrollViewer>
                    </TreeViewItem>
                </StackPanel>
            </HierarchicalDataTemplate>
            <!--un DataTemplate pour le niveau N0(class GE)-->
            <DataTemplate 
                    DataType="{x:Type local:GrandeEntreprise}">
                <TextBlock 
                        FontSize="12"
                        Foreground="WhiteSmoke"
                        Text="{Binding Path=Name}"/>
            </DataTemplate>
        </Application.Resources>
    </Application>
    pj: 4 fichiers texte(secteur activite)
    bon code.............

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

Discussions similaires

  1. Importer des données depuis un fichier texte
    Par Tofalu dans le forum Contribuez
    Réponses: 1
    Dernier message: 28/07/2013, 10h55
  2. extraire des données depuis un fichier text
    Par marouene_ dans le forum Général Java
    Réponses: 1
    Dernier message: 22/06/2011, 00h23
  3. [Batch] Crée des variables depuis un fichier texte
    Par tidou95220 dans le forum Scripts/Batch
    Réponses: 0
    Dernier message: 24/03/2011, 15h26
  4. Réponses: 2
    Dernier message: 23/12/2009, 13h33
  5. Réponses: 0
    Dernier message: 11/02/2008, 11h37

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