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 :

convertir valeur string en Color


Sujet :

Windows Presentation Foundation

  1. #1
    Membre émérite

    Homme Profil pro
    Software Developer
    Inscrit en
    Mars 2008
    Messages
    1 470
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Software Developer

    Informations forums :
    Inscription : Mars 2008
    Messages : 1 470
    Points : 2 368
    Points
    2 368
    Par défaut convertir valeur string en Color
    Bonjour,

    Je stocke une liste de couleur dans un xml mais je ne peux pas convertir ces valeur String en couleur.

    Par exemple je voudrais faire :

    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    monButton.BackGround = maCouleurXmlDeTypeString (avec maCouleurXmlDeTypeString = "Blue")

    Une idée ?

  2. #2
    Membre expert
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    2 210
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 2 210
    Points : 3 015
    Points
    3 015
    Par défaut
    Salut,

    tu peux utiliser un BrushConverter :
    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
          BrushConverter conv = new BrushConverter();
          SolidColorBrush brush = conv.ConvertFromString("Blue") as SolidColorBrush;

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 203
    Points : 220
    Points
    220
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ColorConverter.ConvertFromString(string value);

  4. #4
    Membre émérite

    Homme Profil pro
    Software Developer
    Inscrit en
    Mars 2008
    Messages
    1 470
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Software Developer

    Informations forums :
    Inscription : Mars 2008
    Messages : 1 470
    Points : 2 368
    Points
    2 368
    Par défaut
    Merci binoo
    mais maintenant j'ai une autre erreur quand j'appel ma fonction remplirLst :
    Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.
    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
    Private Lbi As New ListBoxItem
        Private Conv As New BrushConverter()
     
        Private Sub remplirLst(ByVal vGrd As Grid, ByVal vLst As ListBox, ByVal vLbl As Label, ByVal vIEMat As IEnumerable(Of XElement))
            If vIEMat.@Name <> "" Then
                vGrd.Visibility = Visibility.Visible
                vLbl.Content = vIEMat.@Name
                vLst.Items.Clear()
     
                For Each XE As XElement In vIEMat.<Item>
                    If vIEMat.@Name = "COULEUR" Then
                        Lbi.Background = Conv.ConvertFromString(XE.@Bg)
                        Lbi.Foreground = Conv.ConvertFromString(XE.@Fg)
                    End If
                    Lbi.Content = XE.@Value
                    vLst.Items.Add(Lbi)
                Next
            Else : vGrd.Visibility = Visibility.Hidden
            End If
        End Sub
    L'erreur
    'InvalidOperationException was unhandled by user code'
    est levée sur la ligne :

  5. #5
    Membre émérite

    Homme Profil pro
    Software Developer
    Inscrit en
    Mars 2008
    Messages
    1 470
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Software Developer

    Informations forums :
    Inscription : Mars 2008
    Messages : 1 470
    Points : 2 368
    Points
    2 368
    Par défaut
    L'erreur ne provient pas de la couleur mais aparemment de la variable Lbi, il ne semble pas aimer que je le déclare en variable private de classe, mais plutot en variable déclarée dans ma fonction

  6. #6
    Membre expert
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    2 210
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 2 210
    Points : 3 015
    Points
    3 015
    Par défaut
    Je pense que tu dois créer une nouvelle instance de ton ListBoxItem à chaque fois que tu veux en ajouter un. Donc dans ton foreach :
    Code vb : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
                For Each XE As XElement In vIEMat.<Item>
                    Lbi As New ListBoxItem
     
                    If vIEMat.@Name = "COULEUR" Then
                        Lbi.Background = Conv.ConvertFromString(XE.@Bg)
                        Lbi.Foreground = Conv.ConvertFromString(XE.@Fg)
                    End If
                    Lbi.Content = XE.@Value
                    vLst.Items.Add(Lbi)
                Next
    En fait un contrôle ne peut avoir qu'un seul parent comme le message le dit.

  7. #7
    Membre émérite

    Homme Profil pro
    Software Developer
    Inscrit en
    Mars 2008
    Messages
    1 470
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Software Developer

    Informations forums :
    Inscription : Mars 2008
    Messages : 1 470
    Points : 2 368
    Points
    2 368
    Par défaut
    Merci ça fonctionne très bien comme ça.

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

Discussions similaires

  1. [Débutant] Convertir une "String" en valeur "numeric"
    Par caroe.lavoie dans le forum ASP.NET
    Réponses: 3
    Dernier message: 09/08/2013, 11h17
  2. Comment convertir une String en Color ?
    Par nounou0018 dans le forum Débuter
    Réponses: 3
    Dernier message: 16/04/2010, 11h11
  3. convertir un string en system.drawing.color
    Par kira-3 dans le forum VB.NET
    Réponses: 1
    Dernier message: 01/05/2009, 09h59
  4. Convertir un string en type property ou object
    Par bencot dans le forum Langage
    Réponses: 2
    Dernier message: 20/11/2004, 20h18

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