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 :

Ajouter un bouton a un datagrid par code


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre très actif
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Par défaut Ajouter un bouton a un datagrid par code
    Bonjour,

    J'essaye d'ajouter un bouton a un datagrid par code pour le mettre en dernier.

    Cependant je n'y arrive pas

    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Button btnWebAppDetail = new Button();
                btnWebAppDetail.Click += new RoutedEventHandler(this.btnWebappDetail_Click);
                btnWebAppDetail.Content = "Détail";
                btnWebAppDetail.Name = "btnWebAppDetail";
                DataGridTemplateColumn tCol = new DataGridTemplateColumn();
                tCol.CellTemplate = new DataTemplate("Button");
                tCol.CellTemplate.VisualTree = btnWebAppDetail;
    L'erreur est sur la dernière ligne, il ne peut pas convertir un Button en FrameworkElementFactory.

    Suis-je sur la mauvaise piste ?

    PS: J'ai réussi à le faire en XAML, mais ça m'intéresse de savoir comment le faire par le code.

  2. #2
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    Citation Envoyé par Math71 Voir le message
    Bonjour,

    J'essaye d'ajouter un bouton a un datagrid par code pour le mettre en dernier.

    Cependant je n'y arrive pas

    suis-je sur la mauvaise piste ?

    .
    Tu es totalement égaré .FrameworkElementFactory est ton ami pour générer un DataTemplate ou un ControlTemplate par code behind.
    code behind.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
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
     
    namespace WpfDgvDetail
    {
        public class Person:INotifyPropertyChanged
        {
            private int id;
     
            public int Id
            {
                get { return id; }
                set { id = value; OnPropChanged("Id"); }
            }
            private string name;
     
            public string Name
            {
                get { return name; }
                set { name = value;OnPropChanged("Name");  }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropChanged(string propName)
            {
                PropertyChangedEventHandler h = PropertyChanged;
                if (h != null)
                    h(this, new PropertyChangedEventArgs(propName));
            }
        }
     
        public class ListPersons:List<Person>
        {
            public ListPersons()
            {
                for (int i = 1; i < 7; i++)
                {
                    Person p = new Person() { Id = i, Name = "person" + i.ToString() };
                   this.Add(p);
                }
     
            }
        }
    }
    Code behind.cs du Form User 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
    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
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Xml.Linq;
    using System.IO;
    using System.Xml;
     
    namespace WpfDgvDetail
    {
        /// <summary>
        /// Logique d'interaction pour Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
     
            private void btnAddTempatedColumn_Click(object sender, RoutedEventArgs e)
            {
     
                DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
     
                templateColumn.CellTemplate = GetDataTemplate();
                this.dgPerson.Columns.Add(templateColumn);
     
            }
     
            DataTemplate GetDataTemplate()
            {
                FrameworkElementFactory fmFactory = new FrameworkElementFactory(typeof(Button));
     
                fmFactory.SetValue(Button.ContentProperty, "Detail  ");
                fmFactory.SetValue(Button.NameProperty, "btnWebAppDetail");
     
     
     
                fmFactory.AddHandler(Button.ClickEvent , new RoutedEventHandler(btnWebAppDetail_Click),true);
                DataTemplate dt = new DataTemplate();
                dt.VisualTree = fmFactory;
                return dt;
            }
            private void btnWebAppDetail_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("hello WebDetail");
            }
     
     
     
     
        }
    }
    et son code 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
    <Window x:Class="WpfDgvDetail.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfDgvDetail"
            Title="Window1" Height="300" Width="300">
        <Window.DataContext>
            <local:ListPersons/>
        </Window.DataContext>
        <StackPanel >
            <Button x:Name="btnAddTempatedColumn" Content="AddButton" Margin="15"
                    HorizontalAlignment="Center" 
                    Click="btnAddTempatedColumn_Click" />
            <DataGrid IsReadOnly="True" AutoGenerateColumns="False" RowDetailsVisibilityMode="Visible" CanUserResizeColumns="True" 
                      CanUserAddRows="False" Name="dgPerson"
                      ItemsSource="{Binding }">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="*"/>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/>
                </DataGrid.Columns>
             </DataGrid> 
        </StackPanel>
    </Window>
    bon code...

  3. #3
    Membre très actif
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Par défaut
    Bonjour,

    Merci pour ce code en effet, j'étais pas du tout partie au bon endroit.
    Je vais étudier tout ça, encore merci.

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

Discussions similaires

  1. comment ajouter un source ODBC pour SQLServer par code
    Par tomy_libre dans le forum Bases de données
    Réponses: 19
    Dernier message: 28/05/2009, 14h00
  2. Modifié le datasource d'un datagrid par code
    Par faucus dans le forum VB 6 et antérieur
    Réponses: 0
    Dernier message: 04/01/2008, 18h04
  3. ajouter un utilisateur au groupe users par code VBA ?
    Par electrosat03 dans le forum Access
    Réponses: 2
    Dernier message: 12/01/2007, 17h00
  4. [VBA-E]ajouter un textbox sur un formulaire par code?
    Par DonKnacki dans le forum Macros et VBA Excel
    Réponses: 20
    Dernier message: 22/03/2006, 09h33
  5. [C#] Remplacer les boutons d'un DataGrid par une image
    Par PascalL dans le forum Windows Forms
    Réponses: 17
    Dernier message: 04/04/2005, 16h07

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