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

C# Discussion :

WPF - Combobox binding sur le nom d'une propriété


Sujet :

C#

  1. #1
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mai 2010
    Messages
    347
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2010
    Messages : 347
    Points : 121
    Points
    121
    Par défaut WPF - Combobox binding sur le nom d'une propriété
    Bonjour,

    petite question à propos d'un binding qui me parait tout con à première vue :
    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
     
    <DockPanel Name="listAffichage"  Grid.Column="0" Grid.Row="0">
                <ComboBox Name="myComboBox" HorizontalAlignment="Left" ItemsSource="{Binding Path=ListElementMenu, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Height="50" >
     
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <ComboBoxItem x:Name="myComboBoxItem">
                                <StackPanel Orientation="Horizontal" >
                                    <Image Source="{Binding Path=CheminImage}" Grid.Column="0"/>
                                    <TextBlock VerticalAlignment="Center" Foreground="{Binding Path=ForegroundColor}" Text="{Binding Path=Name}" Grid.Column="1" />
                                </StackPanel>
                            </ComboBoxItem>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
     
                </ComboBox>
            </DockPanel>
            <Label Grid.Column="1" Name="lblAffichage" Content="{Binding ElementName=myComboBox, Path=SelectedItem.Name, Mode=OneWay}" Foreground="{Binding ElementName=myComboBox, Path=SelectedItem.ForegroundColor, Mode=OneWay}"  VerticalAlignment="Center" HorizontalAlignment="Center"></Label>
    Je cherche à afficher le nom et la couleur de mon élement de menu dans un label en sélectionnant celui-ci.
    Ce binding marche bien si je sélectionne la zone qui se trouve après mon Textblock mais pas en cliquant sur ce même textblock, ce qui est fort fâcheux pour les utilisateurs.

    Est-ce que le soucis vient du ? je ne vois pas par quoi le remplacer.

    Merci

    Feldi

    [EDIT] : suffisait que je poste pour trouver la solution, j'ai viré e combobox item autour de mon stackPanel et du coup ça fonctionne. Mais j'aimerais bien comprendre pourquoi du coup (double objet qui fait qu'il n'arrive pas à récupérer la propriété de l'objet fils ?)

  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

    Ah bah ,ton click de sélection n'atteint pas l' Item(le StackPanel et ses enfants ).
    Il est intercepté par le ComboBoxItem englobant (la valeur par défaut de son HitTest est visible c.à.d Actif ou "intercepteur" ) ....

    On peut tout mettre donc sauf un ComboxItem dans le DataTemplate du ComboBox sauf si son HitTest =false (à toi de tester)
    Idem pour un ListBoxItem

    code .cs du class data exemple :

    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
     
    namespace WpfComboBox
    {
        public class ElementMenu
        {
            public string Name { get; set; }
            public string CheminImage { get; set; }
        }
        public class ListElementMenus : List<ElementMenu>
        {
            public ListElementMenus()
     
            {
     
                this.Add(
                    new ElementMenu() { Name = "Item1", CheminImage ="Images/Koala.jpg" });
                this.Add(
                    new ElementMenu() { Name = "Item2", CheminImage = "Images/Penguins.jpg" });
                this.Add(
                    new ElementMenu() { Name = "Item2", CheminImage = "Resources/Tulips.jpg" });
            }
        }
    }
    le code xaml du form user :
    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
     
     
    <Window x:Class="WpfComboBox.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfComboBox" 
            xmlns:dat="clr-namespace:System.Collections;assembly=mscorlib"
            Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <dat:ArrayList x:Key="src">
                <local:ElementMenu Name="Koala"  CheminImage="Images/Koala.jpg"/>
                <local:ElementMenu Name="Penguin"  CheminImage="Images/Penguins.jpg"/>
                <local:ElementMenu Name="Tulip"  CheminImage="Images/Tulips.jpg"/>
            </dat:ArrayList>
        </Window.Resources>
        <StackPanel  >
            <Label  Name="lblAffichage" Content="{Binding ElementName=myComboBox, Path=SelectedItem.Name, Mode=OneWay}" Foreground="{Binding ElementName=myComboBox, Path=SelectedItem.ForegroundColor, Mode=OneWay}"  VerticalAlignment="Center" HorizontalAlignment="Center"></Label>
            <DockPanel Name="listAffichage" >
                <ComboBox 
                    Name="myComboBox" HorizontalAlignment="Left"  Height="50"  Width="200"
                    ItemsSource="{Binding Source={StaticResource src},
                    UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top"
                    SelectedValuePath="Name"
                    SelectedValue="Name">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <ComboBoxItem x:Name="myComboBoxItem" Background="Magenta"  Width="150"  IsHitTestVisible="True" >
                                <StackPanel Margin="5" Orientation="Horizontal" Width="100" Background="Yellow" >
                                    <Image Width="50"  Source="{Binding Path=CheminImage}" />
                                    <TextBlock  Text="{Binding Path=Name}" VerticalAlignment="Center" Foreground="{Binding Path=ForegroundColor}"  />
                                </StackPanel>
                            </ComboBoxItem>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </DockPanel>
            <Label  Name="lblAffichage2" Content="{Binding ElementName=myListBox, Path=SelectedItem.Name, Mode=OneWay}" Foreground="{Binding ElementName=myComboBox, Path=SelectedItem.ForegroundColor, Mode=OneWay}"  VerticalAlignment="Center" HorizontalAlignment="Center"></Label>
            <DockPanel Name="listAffichage2" >
                <ListBox 
                    Name="myListBox" HorizontalAlignment="Left"  Height="50"  Width="200"
                    ItemsSource="{Binding Source={StaticResource src},
                    UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top"
                    SelectedValuePath="Name"
                    SelectedValue="Name">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <ComboBoxItem x:Name="myListBoxItem" Background="Magenta"  Width="150"  IsHitTestVisible="True" >
                                <StackPanel Margin="5" Orientation="Horizontal" Width="100" Background="Yellow" >
                                    <Image Width="50"  Source="{Binding Path=CheminImage}" />
                                    <TextBlock  Text="{Binding Path=Name}" VerticalAlignment="Center" Foreground="{Binding Path=ForegroundColor}"  />
                                </StackPanel>
                            </ComboBoxItem>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </DockPanel>
        </StackPanel > 
    </Window>
    bon code...

  3. #3
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mai 2010
    Messages
    347
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2010
    Messages : 347
    Points : 121
    Points
    121
    Par défaut
    Effectivement, en passant IsHitTestVisible à false, ça fonctionne comme si il n'y avait pas de comboboxitem.

    Merci à toi !

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

Discussions similaires

  1. test sur le nom d'une propriété
    Par Hefbee dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 19/11/2007, 08h46
  2. Binding sur <h:inputText> dans une boucle
    Par danyboy85 dans le forum JSF
    Réponses: 6
    Dernier message: 29/05/2007, 11h16
  3. [xaml] Bind sur le titre d'une page
    Par despeludo dans le forum Framework .NET
    Réponses: 6
    Dernier message: 21/03/2007, 09h07
  4. test sur le nom d'une fonction
    Par Art19 dans le forum C
    Réponses: 6
    Dernier message: 26/06/2006, 18h29
  5. [XSL] Test sur le nom d'une balise
    Par KibitO dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 18/12/2005, 13h44

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