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 Forms Discussion :

Utiliser une combobox WPF en winform [Débutant]


Sujet :

Windows Forms

  1. #1
    Membre à l'essai
    Homme Profil pro
    Technicien
    Inscrit en
    Juin 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Technicien
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Juin 2014
    Messages : 19
    Points : 20
    Points
    20
    Par défaut Utiliser une combobox WPF en winform
    Bonjour à tous,

    je débute en C# et je programme de petits add-in destinés à Autocad. Souhaitant créer une combobox identique à celle d'autocad, j'ai trouvé ce code qui permet de la faire en WPF : http://adndevblog.typepad.com/autoca...-controls.html

    Seul la partie "Couleur" m'intéresse mais ce n'est pas là le problème !! Je n'arrive pas à réutiliser le code avec une combobox Winform. Toute la base de mon Add-in est en Winform et je ne souhaite pas passer en WPF (pour le moment).

    Voici le code de la combobox :
    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
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
     
     
    // Class that represents the collection of AcadColor
    public class AcadColors : ObservableCollection<AcadColor>
    {
        public AcadColors()
            : base()
        {
            // List of colors to show in the combo box
            // along with their R,G,B values
            Add(new AcadColor("ByLayer", 255, 255, 255));
            Add(new AcadColor("ByBlock", 255, 255, 255));
            Add(new AcadColor("Red", 255, 0, 0));
            Add(new AcadColor("Yellow", 255, 255, 0));
            Add(new AcadColor("Green", 0, 255, 0));
            Add(new AcadColor("Cyan", 0, 255, 255));
            Add(new AcadColor("Blue", 0, 0, 255));
            Add(new AcadColor("Magenta", 255, 0, 255));
            Add(new AcadColor("White", 255, 255, 255));
            Add(new AcadColor("Select Colors...", 255, 255, 255));
        }
     
         // To check if a color with a given name
        // exists in this collection
        public bool Contains(String name)
        {
            for(int i = 0; i < Count; i++)
            {
                if(this[i].Name.Equals(name))
                    return true;
            }
            return false;
        }
    }
     
     // Class that represents a single color
    public class AcadColor
    {
        // Color Name
        public String Name
        {
            get;
            set;
        }
     
        // Brush to bind the label that displays the color
        public System.Windows.Media.SolidColorBrush ColorBrush
        {
            get
            {
                SolidColorBrush brush
                    = new SolidColorBrush(
                                   Color.FromRgb(Red, Green, Blue));
                return brush; 
            }
        }
     
        // Red byte
        public byte Red
        {
            get;
            set;
        }
     
        // Green byte
        public byte Green
        {
            get;
            set;
        }
     
        // Blue byte
        public byte Blue
        {
            get;
            set;
        }
     
        // The Color combo displays a "Select color..." option
        // In that case, we do not want to show the color label
     
        public String IsVisible
        {
            get
            {
                if(Name.Contains("Select"))
                    return "Collapsed";
                return "Visible";
            }
        }
     
        public AcadColor(String name, byte R, byte G, byte B)
        {
            Name = name;
            Red = R;
            Green = G;
            Blue = B;
        }
     
        public AcadColor()
        {
            Name = String.Empty;
            Red = 255;
            Green = 255;
            Blue = 255;
        }
    }
     
    // View Model for the Color Combo box to bind to
    public class ColorViewModel : INotifyPropertyChanged
    {
        // collection of colors that we will display
        AcadColors _myAcadColors = new AcadColors();
     
        // Flag to know if we are displaying the color selection dialog
        bool _showingColorDlg = false;
     
        public ColorViewModel()
        {
        }
     
        // Bound to the combo box source
        public AcadColors MyAcadColors
        {
            get
            {
                return _myAcadColors;
            }
        }
     
        // Default value to show
        private string _colorString = "Red";
     
        // Bound to the current selection in the combo
        public string ColorString
        {
            get
            {
                return _colorString;
            }
            set
            {
                if (_colorString != value)
                {
                    String colorName = value;
     
                    //The Color combo displays a "Select color..."
                    // option. If that is the option chosen,
                    // show the color pick dialog
                    if (! _showingColorDlg && value.Contains("Select"))
                    {// Show the color pick dialog
                        _showingColorDlg = true;
                        colorName = ShowColorDlg();
                    }
                    _showingColorDlg = false;
                    _colorString = colorName;
                    NotifyPropertyChanged("ColorString");
                }
            }
        }
     
        #region INotifyPropertyChanged Members
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                        new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
     
        // Displays the AutoCAD color pick dialog
        // Method returns the name to be displayed in the combo box
        public String ShowColorDlg()
        {
            Document  doc
                    = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Autodesk.AutoCAD.Windows.ColorDialog dlg
                    = new Autodesk.AutoCAD.Windows.ColorDialog();
            if (dlg.ShowDialog() !=  System.Windows.Forms.DialogResult.OK)
            {
                return String.Empty;
            }
     
            Autodesk.AutoCAD.Colors.Color clr = dlg.Color;
            if (! clr.IsByAci)
            {
                 if (clr.IsByLayer)
                {
                    return "ByLayer";
                }
                else if (clr.IsByBlock)
                {
                    return "ByBlock";
                }
                else
                {
                    String colorName
                        = String.Format("{0},{1},{2}",
                                    clr.Red, clr.Green, clr.Blue);
     
                    if(_myAcadColors.Contains(colorName) == false)
                        _myAcadColors.Insert(
                            _myAcadColors.Count - 1,
                            new AcadColor(
                                            colorName,
                                            clr.Red,
                                            clr.Green,
                                            clr.Blue
                                         ));
                    return colorName;
                }
            }
            else
            {
                // Get the AutoCAD color index
                short colIndex = clr.ColorIndex;
     
                System.Byte byt = System.Convert.ToByte(colIndex);
                int rgb
                    = Autodesk.AutoCAD.Colors.EntityColor.LookUpRgb(byt);
                long b = (rgb & 0xffL);
                long g = (rgb & 0xff00L) >> 8;
                long r = rgb >> 16;
     
                String colorName = String.Format("Color {0}", colIndex);
     
                if (_myAcadColors.Contains(colorName) == false)
                    _myAcadColors.Insert(
                        _myAcadColors.Count - 1,
                        new AcadColor(
                                        colorName,
                                        (byte)r, (byte)g, (byte)b));
     
                return colorName;
            }
        }
    }
    Code WPF :
    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
     
    <ComboBox   DataContext="{Binding ColorDataContext}"
                Name="MyColorCombo"
                Margin="2" Height="30" Width="250"
                ItemsSource="{Binding MyAcadColors}"
                SelectedValuePath="Name"
                SelectedValue="{Binding ColorString}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Margin="2">
                    <Label  Visibility="{Binding IsVisible}"
                            Background="{Binding ColorBrush}"
                            Width="15" Height="15"
                            BorderBrush="Black" BorderThickness="1"/>
                    <TextBlock  Visibility="{Binding IsVisible}"
                                Text=" " Width="3"/>
                    <TextBlock Text="{Binding Name}" Width="100"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    Merci d'avance pour votre aide

    Salutations !

  2. #2
    Membre éprouvé
    Homme Profil pro
    Architecte technique
    Inscrit en
    Septembre 2005
    Messages
    462
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 462
    Points : 1 056
    Points
    1 056
    Par défaut
    Salut,

    Tu es en train de mélanger WPF et WinForm...
    Le "{Bingind ...}" n'existe pas en WinForm !

    Voila plutôt a quoi ressemble le DataBinding en WinForm :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    .....
    grdOrders.DataSource = dsView;
    grdOrders.DataMember = "Customers.RelCustOrd";
    ----------------
    txtExemple.Text = "Le texte";
    ....
    DataBinding WinForm

  3. #3
    Membre à l'essai
    Homme Profil pro
    Technicien
    Inscrit en
    Juin 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Technicien
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Juin 2014
    Messages : 19
    Points : 20
    Points
    20
    Par défaut
    Salut,

    je sais que le deuxième code indiqué correspond au WPF. C'est justement le problème, je voudrai réutiliser le code de la combobox en winform et non pas wpf comme ci dessus.

    Je vais regarder en détail ton lien.
    Merci pour ta réponse.

  4. #4
    Membre à l'essai
    Homme Profil pro
    Technicien
    Inscrit en
    Juin 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Technicien
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Juin 2014
    Messages : 19
    Points : 20
    Points
    20
    Par défaut
    Finalement la meilleure solution est de créer son composant en WPF et de l'intégrer tel quel via ElementHost dans la winform ! C'est tout bête !!

    Merci quand même KatKiller.

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

Discussions similaires

  1. [Débutant] Combobox WPF dans WinForm
    Par Wakabayashi dans le forum Windows Presentation Foundation
    Réponses: 1
    Dernier message: 30/03/2015, 18h14
  2. Utiliser UserControl WinForm dans une fenetre WPF
    Par AliaAtreides dans le forum VB.NET
    Réponses: 2
    Dernier message: 14/01/2013, 17h08
  3. Utiliser une fonction dans un winForm
    Par benny-blanco dans le forum VB.NET
    Réponses: 4
    Dernier message: 10/03/2012, 18h44
  4. [XL-2003] utiliser une combobox a partir d'une autre combobox
    Par fullmetalknet dans le forum Macros et VBA Excel
    Réponses: 13
    Dernier message: 04/08/2009, 14h02

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