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 :

MVVM ListView et DataBinding


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    66
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations forums :
    Inscription : Mars 2005
    Messages : 66
    Par défaut MVVM ListView et DataBinding
    Bonjour,

    Je découvre le design pattern MVVM et en essayant de l'appliquer je suis tombé sur un problème...

    Mon programme génère une liste d'objet à partir d'un fichier XML.
    J'ai donc créer une fabrique d'objet que j'instancie en fonction du fichier choisi par l'utilisateur . Cette fabrique me renvoie une liste d'objets métiers.

    A partir de ceux-ci j'instancie le ViewModel correspondant, instancie un UserControl, le bind avec le ViewModel, et l'ajoute à ma fenêtre.

    Mon problème est que chacune des items de la liste est bindé directement avec l'objet lui même et pas le ViewModel de l'objet...

    Des suggestions ?

  2. #2
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par KoRiGaN44 Voir le message
    Mon problème est que chacune des items de la liste est bindé directement avec l'objet lui même et pas le ViewModel de l'objet...
    Un peu flou. Un peu de code s'il te plait.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    66
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations forums :
    Inscription : Mars 2005
    Messages : 66
    Par défaut
    Vue de mon objet
    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
     
    <UserControl x:Class="ThreeSixty.SLAViewer.View.SLAView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="226" Width="225">
        <GroupBox Header="{Binding Path=name}" >
            <Grid>
                <TextBlock Text="Type de SLA" Height="20" Margin="0,0,0,182" VerticalAlignment="Bottom" />
                <ComboBox SelectedItem="{Binding Path=kind}" ItemsSource="{Binding Path=EnumValues}" Margin="0,0,6,182" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100" />
                <TextBlock Text="Durée" Height="20" Margin="0,0,0,160" VerticalAlignment="Bottom" />
                <TextBox Text="{Binding Path=typicalActivityDuration}" Margin="0,0,6,160" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100" />
                <TextBlock Text="Valeur de base" Height="20" Margin="0,0,0,138" VerticalAlignment="Bottom" />
                <TextBox Text="{Binding Path=baseValue}" Margin="0,0,6,138" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100" />
                <TextBlock Text="Start Proportion" Height="20" Margin="0,0,0,116" VerticalAlignment="Bottom" />
                <TextBox Text="{Binding Path=startProportion}" Margin="0,0,6,116" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100" />
                <TextBlock Text="End Proportion" Height="20" Margin="0,0,0,94" VerticalAlignment="Bottom" />
                <TextBox Text="{Binding Path=endProportion}" Margin="0,0,6,94" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100" />
                <TextBlock Text="Curve Shape" Height="20" Margin="0,0,0,72" VerticalAlignment="Bottom" />
                <TextBox Text="{Binding Path=curveShape}" Margin="0,0,6,72" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100"/>
                <TextBlock Text="Ageing Factor" Height="20" Margin="0,0,0,50" VerticalAlignment="Bottom" />
                <TextBox Text="{Binding Path=ageingFactor}" Margin="0,0,6,50" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100"/>
                <TextBlock Text="Début du SLA" Height="20" Margin="0,0,0,28" VerticalAlignment="Bottom" />
                <TextBox Text="{Binding Path=_start}" Margin="0,0,6,28" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100" />
                <TextBlock Text="Fin du SLA" Height="20" Margin="0,0,0,6" VerticalAlignment="Bottom" />
                <TextBox Text="{Binding Path=_end}" Margin="0,0,6,6" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="100" />
            </Grid>
        </GroupBox>
    </UserControl>
    Vue de la liste d'objets
    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
    <UserControl x:Class="ThreeSixty.SLAViewer.View.SLAListView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        >
        <DockPanel HorizontalAlignment="Left">
            <Button DockPanel.Dock="Top">Ajouter un SLA</Button>
            <ListView ItemsSource="{Binding Path=mySLAs}" DockPanel.Dock="Bottom" AllowDrop="True">
                <ListView.ItemTemplate >
                    <DataTemplate >
                        <myCtrl:SLAView/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </DockPanel>
    </UserControl>
    ViewModel de ma liste d'objets
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    namespace ThreeSixty.SLAViewer.ViewModel
    {
     
        public class SLAListViewModel : BaseViewModel
        {
            public ObservableCollection<SLA> mySLAs {get; set;}
     
            public SLAListViewModel(List<SLA> mySLAList)
            {
                mySLAs = new ObservableCollection<SLA>(mySLAList);
            }
        }
    }
    Ce qui fait que la vue d'un objet est bindée avec l'object métier et pas le ViewModel de l'objet métier.

    Suis-je clair ?

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    66
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations forums :
    Inscription : Mars 2005
    Messages : 66
    Par défaut
    Non ? ça n'inspire personne ?

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

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Par défaut
    Ben, je vois pas où est le pb....

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    66
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations forums :
    Inscription : Mars 2005
    Messages : 66
    Par défaut
    Bah le problème est que mes SLAView ne sont pas bindés avec des SLAViewModele mais avec le SLA directement, etant donné que je donne comme itemsource de ma listview une List<SLA>.

    Est ce que mon SLAListViewModel devrait plutôt avoir un List<SLAViewModel> ? ça me semble un peu étrange, non ?

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

Discussions similaires

  1. [Débutant] C# mvvm listview double click and close window
    Par lucdef dans le forum C#
    Réponses: 6
    Dernier message: 09/04/2014, 15h53
  2. MVVM Listview SelectedItems Binding
    Par gridin dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 06/04/2011, 15h51
  3. [C#][MVVM] ListView, element sur des colonnes
    Par Monkey56 dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 17/09/2010, 14h47
  4. [WPF] Listview tri et databinding
    Par Jérem22 dans le forum Windows Presentation Foundation
    Réponses: 9
    Dernier message: 26/09/2007, 14h08
  5. [C# VS2005 CF]databinding liste d'objets sur un listview
    Par pdesoil dans le forum Windows Forms
    Réponses: 1
    Dernier message: 28/03/2007, 15h38

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