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 :

ComboBox et Binding [Débutant]


Sujet :

Windows Presentation Foundation

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2015
    Messages : 3
    Points : 1
    Points
    1
    Par défaut ComboBox et Binding
    Bonjour à tous,

    Je débute dans l'utilisation des Bindings et ma question doit surement être simple mais je reste bloqué la dessus depuis 2 jours... :

    J'ai 2 ComboBox qui ressemble en gros à ça :

    ComboBox 1 :
    - AAA
    - BBB
    - CCC

    ComboBox 2 :
    - 111
    - 222
    - 333

    Je souhaiterai que lorsque la comboBox 1 à pour valeur AAA, que la comboBox 2 prenne automatiquement pour valeur 111 et que la case soit grisée de manière à ce que l'utilisateur ne puisse plus la modifier tant que la comboBox 1 reste sur AAA. Le reste du temps, les comboBox doivent rester indépendantes.

    Toute aide sera la bienvenue

  2. #2
    Membre du Club
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2009
    Messages
    24
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Juillet 2009
    Messages : 24
    Points : 51
    Points
    51
    Par défaut
    Sur la combobox 2 je ferais un binding sur le IsEnabled

    IsEnabled ="{Binding ComboboxEnabled}"

    et je ferais un binding sur le selectedItem

    SelectedItem="{Binding SelectedValue, Mode=TwoWay}"


    Ensuite:

    Sur la combobox 1 lors de l'event SelectionChanged, tu changes le SelectedItem et tu mets le ComboboxEnabled a false

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2015
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Voila donc ce que j'obtiens dans mon MainWindow.xaml :
    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
     
    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication1="clr-namespace:WpfApplication1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
     
        <Grid d:DataContext="{d:DesignData }">
            <Grid.Resources>
                <wpfApplication1:MainWindow x:Key="MyDataSource"/>
            </Grid.Resources>
     
            <ComboBox Name="ComboBox1" Margin="97,58,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="ComboBox_SelectionChanged">
     
                <ComboBoxItem Content="111&#xD;&#xA;" HorizontalAlignment="Left" Width="118"/>
                <ComboBoxItem Content="222&#xD;&#xA;" HorizontalAlignment="Left" Width="118"/>
                <ComboBoxItem Content="333&#xD;&#xA;" HorizontalAlignment="Left" Width="118"/>
            </ComboBox>
     
            <ComboBox Name="ComboBox2" HorizontalAlignment="Left" Height="20" Margin="209,112,0,0" VerticalAlignment="Top" Width="167" 
                      IsEnabled ="{Binding ComboboxEnabled}"
                      SelectedItem="{Binding SelectedValue, Mode=TwoWay}">
                <ComboBoxItem Content="AAA" HorizontalAlignment="Left" Width="165"/>
                <ComboBoxItem Content="BBB" HorizontalAlignment="Left" Width="165"/>
                <ComboBoxItem Content="CCC" HorizontalAlignment="Left" Width="165"/>
            </ComboBox>
     
     
        </Grid>
    </Window>

    Et ce que j'obtiens dans mon MainWindow.xaml.vb :

    Class MainWindow
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
      Private Sub ComboBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
        Console.WriteLine(Me.ComboBox1.SelectedItem.Content)
        If (Me.ComboBox1.SelectedItem.Content = "111") Then
          Me.ComboBox2.SelectedItem.Content = "AAA"
          Me.ComboBox2.IsEnabled = False
        End If
     
      End Sub
    End Class
    Cependant, la condition "If" n'est jamais validée alors que le Console.WriteLine m'affiche bien "111"...

  4. #4
    Nouveau Candidat au Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2015
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    C'est bon, j'ai trouvé mon erreur et tout fonctionne à présent.

    Merci !

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

Discussions similaires

  1. SelectedItem Combobox XAML binding
    Par hbespoir2003 dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 21/08/2011, 21h08
  2. Combobox SelectedValue Binding
    Par DaMo` dans le forum Silverlight
    Réponses: 2
    Dernier message: 09/08/2010, 18h01
  3. Combobox data binding
    Par Golzinne dans le forum Silverlight
    Réponses: 12
    Dernier message: 06/11/2009, 23h40
  4. Combobox constant binding avec un champ de db
    Par loulouklm dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 12/05/2009, 18h18

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