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 :

Binding image depuis un chemin [Débutant]


Sujet :

Windows Presentation Foundation

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2016
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2016
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Binding image depuis un chemin
    Bonjour,
    je débute en wpf et j'ai quelque soucis avec la notion de binding

    j'ai une base de données (access) avec une table comprenant des noms d'objets, ainsi que le chemin d'images qui leur est associées
    ex :
    id (numAuto)
    nom (texte) : tube
    fichier (texte) : c:\appli\tubE.jpg

    le but étant de d'afficher dynamiquement dans une listbox les objets avec leur nom, et l'image à côté.
    or... j'arrive à récuperer le nom et l'afficher. je récupère également le chemin (que j'arrive à afficher dans un textblox), mais impossible d'afficher l'image

    code xaml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
                <ListBox Margin="10" ItemsSource="{Binding Path=boite}" Name="lst">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="{Binding Path=fichier}"/>
                                <TextBlock Text="{Binding Path=nom}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
    code vb
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
        Private Sub loadList()
            Dim sql = "select * from boite;"
            Dim cmd As New OleDbCommand(Sql, cn)
            Dim dst As New DataSet()
            Dim adpt As New OleDbDataAdapter
            cn.Open()
            adpt.SelectCommand = cmd
            adpt.Fill(dst, "boite")
            lst.DataContext = dst
            cn.Close()
        End Sub
    si quelqu'un à la solution, je suis preneur
    merci par ava,ce

  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

    Le chemin designe par la prop Source du Control Image est de Type "Uri"...non un string ...
    Il faut un donc un chemin Uri par Path et comme MsAccess ne gere pas de type Uri,il faut un class Data wrapper entre la BD et l'interface:

    code .vb du class exemple wrapper Etudiant :
    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
     
     
    Imports System.ComponentModel
     
     
    Public Class Etudiant
        Implements INotifyPropertyChanged
     
        Sub New()
        End Sub
        Private _nom As String
        Public Property Nom() As String
            Get
                Return _nom
            End Get
            Set(ByVal value As String)
                _nom = value
                OnPropertyChanged(New PropertyChangedEventArgs("Nom"))
            End Set
        End Property
        Private _path As String
        Public Property Path() As String
            Get
                Return _path
            End Get
            Set(ByVal value As String)
                _path = value
                OnPropertyChanged(New PropertyChangedEventArgs("Path"))
            End Set
        End Property
      'la prop UriPhoto ,chemin uri, est auto-genere à partir de la prop Path
        'le control image sera binde à cette prop
        Public ReadOnly Property UriPhoto() As Uri
            Get
                Return New Uri(Path, UriKind.RelativeOrAbsolute)
            End Get
     
        End Property
        Public Sub New(ByVal xNom As String,
                       ByVal xPath As String)
            Me.Nom = xNom
            Me.Path = xPath
        End Sub
     
        Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
        Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
            If Not PropertyChangedEvent Is Nothing Then
                RaiseEvent PropertyChanged(Me, e)
            End If
        End Sub
    End Class
    De plus il faut un reader pour le list(of Etudiant) ce qui necessite un reader ...

    code xaml du form:
    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
     
     
    <Window x:Class="WinStorePhotos"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WinStorePhotos" Height="300" Width="300">
        <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Button x:Name="cmdLoadList" Content="LoadList" Click="cmdLoadList_Click" Grid.Row="0"/>
            <ListBox 
               Grid.Row="1"
               Margin="10" ItemsSource="{Binding }" Name="lst">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Width="120" Text="{Binding Path=Nom}"/>
                            <Image Width="50" Source="{Binding Path=UriPhoto}"/>
     
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Window>
    ET code .vb du form:
    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
     
    Public Class WinStorePhotos
        Private cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\OMAR\Desktop\WpfAppStoreDB\BDPHOTOS.mdb"
     
        Private Sub cmdLoadList_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
     
     
            LoadList(cnString)
            lst.DataContext = LoadList(cnString)
        End Sub
        Private Function LoadList(ByVal connectionString) As List(Of Etudiant)
     
     
     
     
     
            Dim cn As New OleDbConnection(connectionString)
            Dim sqlString = "select * from photos;"
            Dim cmd As New OleDbCommand(sqlString, cn)
            Dim ListEtudiants As New List(Of Etudiant)()
            Try
                cn.Open()
                Dim reader As OleDbDataReader = cmd.ExecuteReader()
                Do While reader.Read()
                    Dim etudiant As New Etudiant() With {
                        .Nom = CType(reader("Nom"), String),
                        .Path = CType(reader("Photo"), String)}
     
                    ' Add to collection
                    ListEtudiants.Add(etudiant)
                Loop
            Finally
                cn.Close()
            End Try
            Return ListEtudiants
        End Function
     
     
    End Class
    bon code...

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

Discussions similaires

  1. Réponses: 3
    Dernier message: 06/08/2013, 12h43
  2. Charger une image depuis un textfield [noob]
    Par yodark dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 21/04/2006, 08h36
  3. [C#] Comment inserer Image depuis une ressource ?
    Par EagleEye dans le forum Windows Forms
    Réponses: 2
    Dernier message: 24/01/2006, 13h09
  4. [C#] Récupération d'une image depuis une table SQL Server
    Par borgfabr dans le forum Accès aux données
    Réponses: 10
    Dernier message: 08/04/2004, 13h20

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