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 :

XML + ListView + parcourir Combobox


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2015
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Service public

    Informations forums :
    Inscription : Juillet 2015
    Messages : 40
    Par défaut XML + ListView + parcourir Combobox
    Bonjour à tous,

    Je possède un listView dont je veux pouvoir récupérer dans le code la colonne ID et la valeur sélectionné dans le combobox.

    Voici mes codes:

    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
     
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="ID" Width="80">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Label Content="{Binding XPath=Id}"  />
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Header="Limitation" Width="300">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel>
                                            <ComboBox>
                                                <ComboBoxItem>Aucune</ComboBoxItem>
                                                <ComboBoxItem>Légère</ComboBoxItem>
                                                <ComboBoxItem>Modérée</ComboBoxItem>
                                                <ComboBoxItem>Forte</ComboBoxItem>
                                                <ComboBoxItem>Totale</ComboBoxItem>
                                            </ComboBox>
                                        </StackPanel>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
                foreach(var limits in this.MyListView.Items)
                {
                    Console.WriteLine(limits);
                }
    A l'heure actuelle, "normal", il me retourne dans la console System.Xml.XMLElements.

    Comment je peux faire pour récupérer la valeur de l'ID, et la valeur du Combobox ?

    Merci à vous

  2. #2
    Membre Expert
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    1 562
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 562
    Par défaut
    solution la plus simple
    <ComboBoxItem tag="1">Aucune</ComboBoxItem>

    après je vais te redire ce que je t'ai déjà dit (faire une enum et binder dessus)

    pour ca faudrait que tu bind le selecteditem de la combo sur quelque chose ...

  3. #3
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2015
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Service public

    Informations forums :
    Inscription : Juillet 2015
    Messages : 40
    Par défaut
    Salut,

    Je n'arrive pas à binder dessus:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    - Inconsistent accessibility: base class 'ObservableCollection<Limitation>' is less accessible than class 'Limitations'	- ICF-Form\ICF-Form\Limitations.cs	38
    - The name "limitations" does not exist in the namespace "clr-namespace:ICF_Form".	- MainWindow.xaml	15
    Voici mes codes:

    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
    48
    49
    50
    51
     
    using System.Collections.ObjectModel;
    using System.ComponentModel;
     
    namespace ICF_Form
    {
        enum EnumLimitation
        {
            Aucune,
            Légère,
            Modérée,
            Forte,
            Totale
        }
        class Limitation : INotifyPropertyChanged
        {
           public int _id { get; set; }
     
            private EnumLimitation _enumLimitation = EnumLimitation.Modérée;
            public EnumLimitation _limitation
            {
                get { return _enumLimitation; }
                set
                {
                    _enumLimitation = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("Limitation"));
                }
            }
            public Limitation() { }
            public Limitation(int id = 0)
            {
                _id = id;
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
        }
     
        public class Limitations : ObservableCollection<Limitation>
        {
            public override string ToString()
            {
                string s = "";
                foreach (var item in this)
                {
                    s += item.ToString() + "\n";
                }
                return s;
            }
        }
    }
    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
     
    using System;
    using System.Windows;
     
    namespace ICF_Form
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                Limitations limitations = new Limitations();
                limitations.Add(new Limitation(4)); // ??
                limitations.Add(new Limitation(5)); // ??
                this.DataContext = limitations;
            }
        }
    }
    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
     
    <Window x:Class="ICF_Form.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:System="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:ICF_Form"
            mc:Ignorable="d"
        <Window.Resources>
            <XmlDataProvider x:Key="XmlData" Source="limits.xml"/>
            <ObjectDataProvider x:Key="limitFromEnum" MethodName="GetValues"
                                ObjectType="{x:Type System:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="local:limitations"/>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    <ListView ItemsSource="{Binding Source={StaticResource XmlData},XPath=Limits/*}" Name="MyListView" Grid.Row="2">
                            <GridViewColumn Header="Limitation" Width="300">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel>
                                            <ComboBox Name="LimitCombobox"  ItemsSource="{Binding Source={StaticResource limitFromEnum}}" SelectedItem="{Binding Path=Limitation}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>

  4. #4
    Membre Expert
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    1 562
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 562
    Par défaut
    donne quelques lignes de ton fichier xml stp

  5. #5
    Membre Expert
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    1 562
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 562
    Par défaut
    bon je te met tout le code que j'ai fait
    ca resoud certain problemes mais ca initialise pas la combo au depart
    j'ai pas regardé pourquoi vu que je l'ai vite fait
    xml
    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
    <?xml version="1.0" encoding="utf-8" ?>
    <limits xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <limit>
        <Id>1</Id>
        <Name>Arizona Cardinals</Name>
        <Conference>NFC West</Conference>
        <Limitation>Légère</Limitation>
      </limit>
      <limit>
        <Id>2</Id>
        <Name>Atlanta Falcons</Name>
        <Conference>NFC South</Conference>
        <Limitation>Totale</Limitation>
      </limit>
     <limit>
        <Id>3</Id>
        <Name>Atlanta Falcons</Name>
        <Conference>NFC South</Conference>
        <Limitation>Forte</Limitation>
      </limit>
    </limits>
    code de classes
    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
     public enum EnumLimitation
        {
            Aucune,
            Légère,
            Modérée,
            Forte,
            Totale,
        }
     
        public class Limitation : INotifyPropertyChanged
        {
            public int _id { get; set; }
     
            private EnumLimitation _enumLimitation = EnumLimitation.Modérée;
            public EnumLimitation _limitation
            {
                get { return _enumLimitation; }
                set
                {
                    _enumLimitation = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("Limitation"));
                }
            }
            public Limitation() { }
            public Limitation(int id = 0)
            {
                _id = id;
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
        }
     
        public class Limitations : ObservableCollection<Limitation>
        {
            public override string ToString()
            {
                string s = "";
                foreach (var item in this)
                {
                    s += item.ToString() + "\n";
                }
                return s;
            }
        }
    code window
    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
      public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                string appPath = System.IO.Path.GetDirectoryName(
                    System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
                XmlData.Source = new Uri(appPath + @"\limits.xml");
            }
            protected override void OnClosed(EventArgs e)
            {
                string source = XmlData.Source.LocalPath;
                XmlData.Document.Save(source);
                base.OnClosed(e);
            }
        }
    code xaml
    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
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     <Window.Resources>
            <!--<XmlDataProvider x:Key="XmlData"
                             Source="limits.xml"
                             XPath="limits/limit" />-->
            <ObjectDataProvider x:Key="limitFromEnum"
                                MethodName="GetValues"
                                ObjectType="{x:Type System:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="local:EnumLimitation" />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>
        <Grid>
            <Grid.DataContext>
                <XmlDataProvider x:Name="XmlData"
                                 Source="limits.xml"
                                 XPath="limits/limit" />
            </Grid.DataContext>
            <!--<ComboBox Name="LimitCombobox"
                      ItemsSource="{Binding Source={StaticResource limitFromEnum}}"
                      SelectedItem="{Binding Path=Limitation}" />-->
            <ListView ItemsSource="{Binding .}"
                      Margin="2"
                      IsSynchronizedWithCurrentItem="True"
                      Background="Aqua">         
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="ID"
                                        Width="100">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding XPath=Id}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn> 
                        <GridViewColumn Header="Limitation "
                                        Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding XPath=Limitation}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Limitation"
                                        Width="300">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <ComboBox Name="LimitCombobox" Width="100"
                                                  ItemsSource="{Binding Source={StaticResource limitFromEnum}}" 
                                                  SelectedValue="{Binding XPath=Limitation}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    ca met a jour directement la limitation dans le xml

  6. #6
    Membre Expert
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    1 562
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 562
    Par défaut
    mais a mon avis tu melage tout entre ton xml et tes classes
    en convertissant ton xml en observablecollection<limitation> ca serait plus simple

Discussions similaires

  1. listView et comboBox
    Par floriaan60 dans le forum Windows Forms
    Réponses: 1
    Dernier message: 10/06/2008, 10h48
  2. Afficher mes données XML avec un Combobox
    Par tatata dans le forum ActionScript 3
    Réponses: 4
    Dernier message: 09/06/2008, 13h57
  3. Réponses: 1
    Dernier message: 30/08/2007, 14h38
  4. [VB6][ListView] ListView et ComboBox
    Par gwendo dans le forum VB 6 et antérieur
    Réponses: 9
    Dernier message: 01/06/2007, 18h49
  5. [C#]ListView et combobox
    Par fafa139 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 22/05/2006, 09h49

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