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 :

DataGrid :: Bind Combo Box avec une traduction


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    57
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 57
    Par défaut DataGrid :: Bind Combo Box avec une traduction
    Bonjour,

    J'ai une classe Data
    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
     
    namespace TestDataGridComboBoxTranslate
    {
        class Data : INotifyPropertyChanged
        {
            private int value;
            private string type;
            public int Value
            {
                get
                {
                    return value;
                }
                set
                {
                    this.value = value;
                    OnPropertyChanged("Diametre");
                }
            }
            public string Type
            {
                get
                {
                    return type;
                }
                set
                {
                    type = value;
                    OnPropertyChanged("Diametre");
                }
            }
     
     
            public Data() { value = 0; type = "default"; }
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(String info)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(info));
                }
            }
     
        }
    }
    Cette classe est binder pour communiquer avec un DataGrid :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    <Window x:Class="TestDataGridComboBoxTranslate.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <DataGrid Name="DataGrid" AutoGenerateColumns="False" Height="311" HorizontalAlignment="Left" VerticalAlignment="Top" Width="503">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="{StaticResource Value}" Binding="{Binding Type}"></DataGridTextColumn>
                    <DataGridTextColumn Header="{StaticResource Type}" Binding="{Binding Value}"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    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
     
    namespace TestDataGridComboBoxTranslate
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            List<Data> listData = new List<Data>();
     
            public MainWindow()
            {
                InitializeComponent();
                DataGrid.ItemsSource = listData;
                var data = new Data();
                data.Value = 69;
                data.Type = "Super";
                listData.Add(data);
            }
        }
    }
    Comme vous pouvez le voir, j'utilise un dictionnaire de ressource pour pouvoir effectuer une traduction de l'application de manière dynamique.

    Le code ci-dessus marche, mais j'aimerai avoir pour Data.type un int sélectionné depuis une combo box. Donc il faut que les champs de la combo box se mettent à jour de manière dynamique.

    Voici le dictionnaire en français anglais:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:lang="clr-namespace:System;assembly=mscorlib">
     
    	<lang:String x:Key="Type">Type</lang:String>
    	<lang:String x:Key="Value">Value</lang:String>
     
    	<lang:String x:Key="Type1">TypeEN1</lang:String>
    	<lang:String x:Key="Type2">TypeEN2</lang:String>
    	<lang:String x:Key="Type3">TypeEN3</lang:String>
    	<lang:String x:Key="Type4">TypeEN4</lang:String>
     
    </ResourceDictionary>
    Par une manière très sale (sans bind, sans utilisé le dictionnaire) j'arrive à un comportement presque fonctionnel. Mais cela ne me satisfait pas.

    J'ai essayè par l'utilisation d'une DataGridComboBoxColumn, DataGridTemplateColumn avec une ComboBox. Mais je n'arrive pas à un résultat correct, car soit le bind ne fonctionnne pas, soit l'application plante, ,soit la traudciton ne se fait pas, soit il ne se passe rien

    Donc j'aimerai un peu d'aide, au moins pour rediriger ma recherche sur le bon composant.
    Merci

    Voici quelque lien utile :
    http://bytes.com/topic/c-sharp/answe...n-datagrid-wpf
    http://msdn.microsoft.com/fr-fr/library/ms752347.aspx
    http://msdn.microsoft.com/fr-fr/libr...vs.100%29.aspx
    http://msdn.microsoft.com/fr-fr/libr...=vs.80%29.aspx
    http://msdn.microsoft.com/fr-fr/libr...boxcolumn.aspx

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    57
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 57
    Par défaut
    J'ai trouvé une solution plutôt correcte à mon problème :
    http://stackoverflow.com/questions/3...ectedvaluepath

    Quelque détail intéressant :
    http://stackoverflow.com/questions/3...ectedvaluepath

    Enfin, il manquait juste l'attribut "SelectedValuePath"

    Par contre, je n'arrive pas à rendre dynamique la valeur afficher par le comboBox en fonction de la langue. En cas de changement de langue, je dois recharger l'ensemble ListLibelle de chaque combo box.

    Peut-on Bind un string vers un champ d'un dictionnaire de ressource?
    J'aimerai quelque chose qui se rapproche de ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    libelles.Add(new Libelle(0, "{DynamicResource Type1}"));

  3. #3
    Membre éclairé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    436
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2006
    Messages : 436
    Par défaut
    Alors pour ta deuxième question dans ta réponse, je ne sais pas. Maintenant je connaissais le comportement plus que douteux des Combobox dans les datagrid ... Vraiment n'importe quoi ... Avec des bindings sur selectedItem, tu peux essayer longtemps ... Puis un jour tu tests avec SelectedValue et ohhhh miracle ! Ca marche ! De mémoire, je n'avais même pas besoin du selectedValuePath mais peut être parce que j'avais surchargé mes méthodes ToString et Equals ...

  4. #4
    Membre actif
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    57
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 57
    Par défaut
    Je confirme, mes premiers tests avec SelectedItem était laborieux

    Le comportement avec les binding est correct maintemant. Il me reste plus qu'à rendre l'affichage du texte du combo box dynamique en fonction de la langue. J'ai donc utilisé les DependanceProperty.

    Avant affichage, je peux modifier la langue et les modifications sont prisent en compte. Mais une fois le combo box affiché, le texte perd sa dynamité.


    Voici la classe Libelle définisant les valeurs du combo box :
    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
    namespace TestDataGridComboBoxTranslate
    {
        public class Libelle : DependencyObject, INotifyPropertyChanged
        {
            //Propriétés
            private int valeur;
            public static readonly DependencyProperty textProperty = DependencyProperty.Register("Text", typeof(string), typeof(Libelle));
     
            public int Valeur
            {
                get
                {
                    return valeur;
                }
                set
                {
                    valeur = value;
                    OnPropertyChanged("Valeur");
                }
     
            }
            public string Text
            {
                get { return (string)GetValue(textProperty); }
                set { SetValue(textProperty, value); }
            }
     
            public Libelle()
            {
                Valeur = 0;
                Text = "Default";
            }
     
            public Libelle(int value, string libelle)
            {
                Valeur = value;
                Text = libelle;
            }
     
     
            //Méthode permettant de gérer les notifications de mises à jours de l'objet
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(String info)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(info));
                }
            }
        }
    }
    Le DataGrid avec la combo box :
    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
     
    <Window x:Class="TestDataGridComboBoxTranslate.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:app="clr-namespace:TestDataGridComboBoxTranslate"
            Title="MainWindow" Height="350" Width="525">
     
        <Grid>
            <DataGrid Name="DataGrid" AutoGenerateColumns="False" Height="311" HorizontalAlignment="Left" VerticalAlignment="Top" Width="503"  ItemsSource="{Binding listData}">
                <DataGrid.Columns>
                    <DataGridComboBoxColumn x:Name="comboBox"  Header="{DynamicResource Type}"
                            SelectedValueBinding="{Binding Type}"
                            SelectedValuePath="Valeur"
                            DisplayMemberPath="Text">
                        <DataGridComboBoxColumn.ItemsSource>
                            <app:ListLibelle>
                                <app:Libelle Valeur="0" Text="{DynamicResource Type1}"/>
                                <app:Libelle Valeur="1" Text="{DynamicResource Type2}"/>
                                <app:Libelle Valeur="2" Text="{DynamicResource Type3}"/>
                            </app:ListLibelle>
                        </DataGridComboBoxColumn.ItemsSource>
                    </DataGridComboBoxColumn>
                    <DataGridTextColumn Header="{DynamicResource Value}" Binding="{Binding Value}"/>
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="Changer Langue" Height="23" HorizontalAlignment="Left" Margin="0,288,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />
            <Button Content="Afficher" Height="23" HorizontalAlignment="Left" Margin="106,288,0,0" Name="button2" VerticalAlignment="Top" Width="100" Click="button2_Click" />
        </Grid>
    </Window>
    Avant d'afficher la fenêtre le comportement de langue est parfait, mais une fois affichée la combo box ne se rafraichit par correctement

  5. #5
    Membre éclairé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    436
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2006
    Messages : 436
    Par défaut
    Essaye de faire ceci :

    Sur l'event déclenché lors du changement de langue :

    (soit "combobox", le x:name de ta combo)

    combobox.GetBindingExpression(DataGridComboBoxColumn.ItemsSource).UpdateSource();

  6. #6
    Membre actif
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    57
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 57
    Par défaut
    Sauf que l'objet est un DataGricComboBoxColumn.
    http://msdn.microsoft.com/fr-fr/libr...boxcolumn.aspx

    Il ne contient pas la méthode GetBindingExpression qui est dans la classe ComboBox.

    Ensuite, ce n'est pas la combo box qui est binder sur le dictionnaire, mais une liste de libellé.

    Mais je comprend ton idée qui est intéressante. Il faudrait que j'abonne le rafraichissement de la source du DataGricComboBoxColumn à une modification du libelle (qui est la source).

    Edit::Nous avons mal localisé le problème. Une fois le combo box afficher, la liste de libellés ne se met plus à jour en fonction de la langue. Hors elle se met correctement à jours avant l'affichage du combo box.

    Si après affichage je change de langue sans dérouler la combo box, certain champ prendront la valeur de la nouvelle langue. Une fois dérouler plus aucun changement possible.

    OverWrite du binding des libellés vers le dictionnaire?
    La propriété WPF Text des libellés est à la fois source et cible.

    Dictionnaire -> Libelle.Text -> DataGridComboBoxColumn.ItemsSources

    Peut-être que ma structure de base est mauvaise

Discussions similaires

  1. Réponses: 7
    Dernier message: 01/02/2015, 19h51
  2. Mettre plusieurs combo box sur une meme ligne
    Par hugo69 dans le forum Ext JS / Sencha
    Réponses: 4
    Dernier message: 13/11/2008, 15h42
  3. Combo box avec cellule liée
    Par andy38 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 13/06/2008, 10h38
  4. Problème de combo box avec additem
    Par Mariquiqui dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 01/04/2008, 19h25
  5. Ecrire dans une text Box avec une mouseMove
    Par popsmelove dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 03/03/2008, 14h00

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