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 :

TextBlock Header Expander


Sujet :

Windows Presentation Foundation

  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 TextBlock Header Expander
    Bonjour,

    j'ai fait un datagrid avec possibilité de regrouper mes valeurs suivant plusieurs critères.

    Nom : 2016-05-26.png
Affichages : 446
Taille : 34,0 Ko

    Le regroupement est fait suivant la colonne "Forme".
    Cependant, au lieu de voir affiché : "Rectangulaire" ou "Circulaire" je souhaiterai obtenir ceci : "Forme : Rectangulaire".

    voici mon code xaml pour le groupement. Que dois-je mettre dans <Expander.Header> ?

    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
     
    <DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Path=Name}" />
                                </StackPanel>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Border BorderBrush="Gray" BorderThickness="0.5" CornerRadius="4" Margin="3">
                                                <Border.Background>
                                                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                                        <GradientStop Color="Wheat" Offset="0"/>
                                                        <GradientStop Color="White" Offset="1"/>
                                                    </LinearGradientBrush>
                                                </Border.Background>
                                                <Expander IsExpanded="True" Style="{StaticResource Fleche}">
                                                    <Expander.Header>
                                                        <TextBlock Text="{Binding Name}"/>
                                                    </Expander.Header>
                                                    <ItemsPresenter/>
                                                </Expander>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </DataGrid.GroupStyle>
    Merci d'avance !


  2. #2
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    255
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2002
    Messages : 255
    Points : 445
    Points
    445
    Par défaut
    Wakabayashi,

    Peut-être simplement ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    <Expander.Header>
      <StackPanel Orientation="Horizontal">
           <TextBlock Text="Forme : "/>
           <TextBlock Text="{Binding Name}"/>
      </StackPanel>
    </Expander.Header>
    Salutations.

  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
    Merci pour ta réponse.

    Oui mais non !!
    Effectivement ça marche mais le groupement dépend de la colonne choisie par l'utilisateur.
    Là c'est "Forme" mais ça pourrait être "Repère".

    Pour grouper mes données j'utilise cette focntion :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    ICollectionView collViewResa = ((CollectionViewSource)this.Resources["cvResa"]).View;
    collViewResa.GroupDescriptions.Add(new PropertyGroupDescription("Forme"));
    La valeur de PropertyGroupDescritpiton dépendra d'un choix utilisateur et c'est ce choix que je souhaite retrouver dans mon Header.


  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
    bonjour

    Comme dans le Path=Name,le Binding renvoie en fait le ToStrinf() de la prop et non son Name ,une astuce est de "capturerr" le nom de la prop ajoutée au CollectionView.GroupDescriptions...

    Ce nom est store au niveau du Form sous forme d'une prop auquel tu bindera le 2eme TextBlock signalé par Fabiani...

    code behind .cs du 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
    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
     
     
    namespace WpfBindingFallBack
    {
        public class Shape : INotifyPropertyChanged
        {
     
             public Shape()
            {
     
     
     
                 Repere = string.Empty;
                Forme = string.Empty;
                Largeur = string.Empty;
                Hauteur = string.Empty;
            }
             public Shape(string prepere, string pforme,double plargeur,double phauteur )
                 : this()
            {
                Repere = prepere;
                Forme = pforme;
                Largeur = plargeur.ToString ();
                Hauteur = phauteur.ToString();
            }
             private string m_repere;
     
             public string Repere
             {
                 get { return m_repere; }
                 set { m_repere = value; OnPropertyChanged("Repere"); }
             }
     
            private string  m_forme;
     
            public string Forme
            {
                get { return m_forme; }
                set { m_forme = value; OnPropertyChanged("Forme"); }
            }
     
            private string m_largeur;
     
            public string Largeur
            {
                get { return m_largeur; }
                set { m_largeur = value; OnPropertyChanged("Largeur"); }
            }
            private string m_hauteur;
     
            public string Hauteur
            {
                get { return m_hauteur; }
                set { m_hauteur = value; OnPropertyChanged("Hauteur"); }
            }
     
     
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
        }
        public class Shapes : ObservableCollection<Shape >
        {
            Shape shp = null;
            Random rnd = new Random();
            public Shapes ()
            {
                for (int i = 1; i < 21; i++)
                {
                    if (rnd.Next(1,101) % 2==0)
                     shp = new Shape(
                        "RSV-" + String.Format("00", i), 
                        "Rectangulaire", rnd.Next(10, 50), rnd.Next(10, 50));
                    else
                        shp = new Shape(
                                           "RSV-" + String.Format("00", i), 
                                           "Circulaire", rnd.Next(10, 50), rnd.Next(10, 50));
                    this.Add(shp);
                }
     
     
            }
        }
    }
    code xaml du form utilisateur:
    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
     
     
    <Window x:Class="WpfBindingFallBack.Window2"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:local="clr-namespace:WpfBindingFallBack" 
            Title="Window2" Height="300" Width="300"
            x:Name="win">
           <!--le form est nomme-->
        <Window.Resources>
            <local:Shapes x:Key="src"/>
     
        </Window.Resources>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="auto"></ColumnDefinition>
                <ColumnDefinition Width="auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <DataGrid 
                x:Name="dgv"
                Grid.Column="0"
                HorizontalAlignment="Stretch" 
                ItemsSource="{StaticResource src}" >
     
                <DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Path=Name}" />
                                </StackPanel>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Border BorderBrush="Gray" BorderThickness="0.5" CornerRadius="4" Margin="3">
                                                <Border.Background>
                                                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                                        <GradientStop Color="Wheat" Offset="0"/>
                                                        <GradientStop Color="White" Offset="1"/>
                                                    </LinearGradientBrush>
                                                </Border.Background>
                                                <Expander
                                                    IsExpanded="True" 
                                                    ExpandDirection="Down" HorizontalAlignment="Stretch"   
                                                          >
                                                    <Expander.Header>
                                                        <StackPanel Orientation="Horizontal">
                                                            <TextBlock Text="{Binding ElementName=win, Path=PropName}"/>
                                                            <TextBlock Text="{Binding Path=Name}"/>
                                                        </StackPanel >
                                                    </Expander.Header>
                                                    <ItemsPresenter/>
                                                </Expander>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </DataGrid.GroupStyle>
     
            </DataGrid> 
            <Button 
                Grid.Column="1"
                HorizontalAlignment="Right"   VerticalAlignment="Center" 
                x:Name="btnGroup1" 
                Content="Group"
                Click="btnGroup1_Click" />
            <Button 
               Grid.Column="2"
                HorizontalAlignment="Right"   VerticalAlignment="Center" 
                x:Name="btnGroup2" 
                Content="Group"
                Click="btnGroup2_Click" />
        </Grid>   
    </Window>
    et son code behind .cs:
    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
     
    namespace WpfBindingFallBack
    {
        /// <summary>
        /// Logique d'interaction pour Window2.xaml
        /// </summary>
        public partial class Window2 : Window
        {
           private  CollectionView myCollectionView = null;
           private PropertyGroupDescription pn = new PropertyGroupDescription();
            public Window2()
            {
                InitializeComponent();
                myCollectionView = (CollectionView)
                     CollectionViewSource.GetDefaultView(this.dgv.ItemsSource);
            }
     
     
            private void btnGroup1_Click(object sender, RoutedEventArgs e)
            {
                myCollectionView.GroupDescriptions.Clear();
     
                pn = new PropertyGroupDescription("Forme");
                myCollectionView.GroupDescriptions.Add(pn);
     
            }
     
            private void btnGroup2_Click(object sender, RoutedEventArgs e)
            {
     
     
                myCollectionView.GroupDescriptions.Clear();
     
                pn = new PropertyGroupDescription("Largeur");
                // pn = new PropertyGroupDescription("Hauteur");
                myCollectionView.GroupDescriptions.Add(pn);
     
     
            }
     
     
            public string PropName
            {
                get { return pn.PropertyName + " : ";  }
            }
     
        }
    }
    bon code.........

  5. #5
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    255
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2002
    Messages : 255
    Points : 445
    Points
    445
    Par défaut
    Wakabayashi,

    On peut intercepter onclick sur le 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
    15
    16
    17
    18
    19
    20
    21
    22
     
     <DataGrid.ColumnHeaderStyle>
      <Style TargetType="DataGridColumnHeader">
           <EventSetter Event="Click" Handler="columnHeader_Click"/>
       </Style>
     </DataGrid.ColumnHeaderStyle>
     
    //...
    <Expander.Header>
    <DockPanel>
     <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=header}"/> 
     <TextBlock Text="{Binding Path=Name}"/>                                             
    </DockPanel>
    </Expander.Header>
     
    //code behind...
          public string header { get; set; }
     
          void columnHeader_Click(object sender, RoutedEventArgs e)
          {
                header = (sender as DataGridColumnHeader).Content.ToString()+ " : ";
          }
    Salutations.

  6. #6
    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
    Merci à vous deux pour vos réponses.

    @ MABROUKI : C'est exactement ça que je cherchais !


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

Discussions similaires

  1. [Expander] Dissocier background du header et content
    Par Truelle dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 05/10/2010, 15h41
  2. [gtkmm/c++] expander header
    Par drKzs dans le forum GTK+ avec C & C++
    Réponses: 2
    Dernier message: 24/07/2009, 12h33
  3. Expander et style du Header
    Par tomlev dans le forum Windows Presentation Foundation
    Réponses: 8
    Dernier message: 12/01/2009, 14h29
  4. mise en page (Header and Footer) en XML-XSL.
    Par christine dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 01/03/2004, 16h31
  5. [Turbo C++] Fonciton containing for are not expanded inline
    Par BuG dans le forum Autres éditeurs
    Réponses: 6
    Dernier message: 17/02/2003, 06h48

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