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 Code grid avec cellule grid


Sujet :

C#

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Webdesigner
    Inscrit en
    Novembre 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Aisne (Picardie)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Novembre 2020
    Messages : 6
    Points : 6
    Points
    6
    Par défaut WPF Code grid avec cellule grid
    Bonjour je souhaite réaliser en code uniquement ce type de grille.

    Code XML : 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
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="1" Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
                <Button Grid.Row="0" Content="Button1"/>
                <Button Grid.Row="1" Content="Button2"/>
                <Button Grid.Row="2" Content="Button3"/>
        </Grid>
    </Grid>

    L'affection SetColumn et SetRow ne fonctionne pas pour la grille interne en code C#.
    Avez-vous une solution pour convertir ce XAML en C#
    Merci

  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
    La voici:
    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
    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.Navigation;
    using System.Windows.Shapes;
     
    namespace WpfCodeBehindGrid
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                SetupGrid();
            }
            private void SetupGrid()
            {
                Grid grd = new Grid();
                grd.Name = "grd";
                grd.ShowGridLines = true;
     
                for (int i = 1; i < 4; i++)
                {
                    RowDefinition rowdef = new RowDefinition();
                    rowdef.Height = new GridLength(1,GridUnitType.Star);
                    grd.RowDefinitions.Add(rowdef);
                    ColumnDefinition coldef = new ColumnDefinition();
                    coldef.Width = new GridLength(1, GridUnitType.Star);
                    grd.ColumnDefinitions.Add(coldef);
                }
                // on ajoute un bouton row=0 et col=0
                Button btn= new Button();
                btn.Name = "btn";
                btn.Content = "bouton exemple";
                btn.HorizontalAlignment = HorizontalAlignment.Stretch;
                btn.VerticalAlignment = VerticalAlignment.Stretch;
                Grid.SetRow(btn, 0); // on utilise la dependency prop attache "static"
                Grid.SetColumn(btn, 0);
                grd.Children.Add(btn);
     
                this.panel.Children.Add(grd);
     
            }
        }
    }
    code xaml du form user:
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <Window x:Class="WpfCodeBehindGrid.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">
        <StackPanel x:Name="panel">
     
        </StackPanel>
    </Window>
    bon code...

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Webdesigner
    Inscrit en
    Novembre 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Aisne (Picardie)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Novembre 2020
    Messages : 6
    Points : 6
    Points
    6
    Par défaut
    Merci de ta réponse mais ce n'est pas le code qui correspond à la demande.
    Le but est d'inséré une grille dans une cellule d'une autre grille.(avec une ou plusieurs colonnes et lignes)
    Ton code pose simplement un bouton dans une grille.
    Je cherche depuis longtemps mais c'est le grid.setrow ou grid setcol de la grille interne qui pose problème.
    Merci si tu peux m'aider.

  4. #4
    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
    Citation Envoyé par chodid Voir le message
    Merci de ta réponse mais ce n'est pas le code qui correspond à la demande.
    Le but est d'inséré une grille dans une cellule d'une autre grille.(avec une ou plusieurs colonnes et lignes)
    Ton code pose simplement un bouton dans une grille.
    Je cherche depuis longtemps mais c'est le grid.setrow ou grid setcol de la grille interne qui pose problème.
    Merci si tu peux m'aider.
    A une heure du matin ,j'ai regardé en diagonale .Alors voici le code complet revu ,qui au passage utilise la même technique que celle du button .
    Sauf qu'il faut taper à la mano beaucoup plus de code et qu'il faut organiser son code ....!!!
    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
     
    namespace WpfCodeBehindGrid
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                SetupAllGrids();
            }
            private void SetupAllGrids()
            {
                // Grid  Externe
                Grid grid1 = GetGrid1();
                this.panel.Children.Add (grid1);
     
     
                //  Grid  Interne
                Grid grid2 = GetGrid2(); ;
                grid1.Children.Add(grid2);
                Grid.SetRow(grid2, 1);
                Grid.SetColumn(grid2, 1);
     
     
                //  liste de buttons
               List<Button> buttons = GetButtons();
     
               for (int i = 0; i < buttons.Count ; i++)
               {
                   Button btn = buttons[i];
                   Grid.SetRow(btn, i);
                   Grid.SetColumn (btn, 0); 
                   grid2.Children.Add(btn);
               }
     
            }
            private Grid GetGrid1()
            {
                Grid grd = new Grid();
                grd.Name = "gridExterne";
                grd.ShowGridLines = true;
     
                for (int i =0; i < 3; i++)
                {
                    RowDefinition rowdef = new RowDefinition();
                    rowdef.Height = new GridLength(1, GridUnitType.Star);
                    grd.RowDefinitions.Add(rowdef);
     
                    ColumnDefinition coldef = new ColumnDefinition();
                    coldef.Width = new GridLength(1, GridUnitType.Star );
                    grd.ColumnDefinitions.Add(coldef);
                }
                return grd;
            }
            private Grid  GetGrid2()
            {
                Grid grd = new Grid();
                grd.Name = "gridInterne";
                grd.ShowGridLines = true;
     
                for (int i =0; i < 3; i++)
                {
                    RowDefinition rowdef = new RowDefinition();
                    rowdef.Height = new GridLength(1, GridUnitType.Star );
                    grd.RowDefinitions.Add(rowdef);
                }
                return grd;
            }
            private List<Button> GetButtons()
            {
                List<Button> buttons = new List<Button>();
                Button btn;
                for (int i = 0; i < 3; i++)
                {
                    btn = new Button();
                    btn.Name = "button" + (i + 1).ToString();
                    btn.Content = "button" + (i + 1).ToString();
                    buttons.Add(btn);
                }
                return buttons;
            }
        }
    }
    code xaml du form user inchangé...
    bon code..

  5. #5
    Futur Membre du Club
    Homme Profil pro
    Webdesigner
    Inscrit en
    Novembre 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Aisne (Picardie)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Novembre 2020
    Messages : 6
    Points : 6
    Points
    6
    Par défaut
    Ok ça fonctionne, merci beaucoup. Pourrais-tu me dire pourquoi il faut absolument un panel de fond ?
    J'ai essayé de mettre par exemple 10 lignes identiques.
    Ce panel même avec les propriétés horizontalAlignment et VerticalAlignment à strech empêche le redimensionnement complet de la grille à la fenêtre Windows en cas de plein écran.
    Y a t-il un moyen de poser la grille mère sur la fenêtre Windows sans utiliser de Panel ?
    Merci te ton investissement.
    Bonne journée.

  6. #6
    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
    Citation Envoyé par chodid Voir le message
    Ok ça fonctionne, merci beaucoup. Pourrais-tu me dire pourquoi il faut absolument un panel de fond ?
    J'ai essayé de mettre par exemple 10 lignes identiques.
    Ce panel même avec les propriétés horizontalAlignment et VerticalAlignment à strech empêche le redimensionnement complet de la grille à la fenêtre Windows en cas de plein écran.
    Y a t-il un moyen de poser la grille mère sur la fenêtre Windows sans utiliser de Panel ?
    Merci te ton investissement.
    Bonne journée.
    J'ai utilisé un StackPanel pour des raisons de pure commodité (economie de frappe en xaml comme en code behind sinon il faut taper des grid.row="" et grid.col= par dizaines sinon centaines).
    On peut utiliser tout control dérivant de Pane pour obtenir le même résultat ,par exemple un simple Grid .
    Si on veut se débarrasser de tout panel et héberger directement ton "grid1" ou Grid le plus externe dans la fenêtre hôte ,le code behind.cs revu serait :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    private void SetupAllGrids()
            {
                // Grid  Externe
                Grid grid1 = GetGrid1();
                //this.panel.Children.Add (grid1); // la ligne commentée sera remplacé par
                // cette ligne 
                this.Content = grid1;
     
                //.....le  reste sans changement
     
    }
    bon code ....

  7. #7
    Futur Membre du Club
    Homme Profil pro
    Webdesigner
    Inscrit en
    Novembre 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Aisne (Picardie)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Novembre 2020
    Messages : 6
    Points : 6
    Points
    6
    Par défaut
    Super, tout fonctionne.
    Maintenant on a une grille responsive pouvant comporter des grilles responsive.
    Reste juste a bien préparer la map générale.
    Merci encore.
    Bien à toi !

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

Discussions similaires

  1. code vba excel copie avec cellule de référence
    Par jaquot14 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 24/10/2020, 18h58
  2. [XL-2007] Code erreur 13 avec cellule qui contient du texte
    Par Audvrd dans le forum Macros et VBA Excel
    Réponses: 13
    Dernier message: 22/06/2018, 17h28
  3. [Débutant] WPF - Utilisation Grid dynamique
    Par Fellerson dans le forum C#
    Réponses: 7
    Dernier message: 12/09/2017, 18h42
  4. [XSLT]tableau double entrée avec cellule manquante
    Par nferay dans le forum XSL/XSLT/XPATH
    Réponses: 9
    Dernier message: 08/03/2005, 15h07
  5. [XSL-FO] Table avec cellule vide
    Par JustAGphy dans le forum XSL/XSLT/XPATH
    Réponses: 6
    Dernier message: 12/05/2004, 14h11

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