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 :

[WPF]masquer image d'un GridViewColumn d'un listView


Sujet :

Windows Presentation Foundation

  1. #1
    Membre à l'essai Avatar de Labardass
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Paris (Île de France)

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

    Informations forums :
    Inscription : Octobre 2013
    Messages : 6
    Points : 11
    Points
    11
    Par défaut [WPF]masquer image d'un GridViewColumn d'un listView
    Bonsoir,
    je chercher a conditionner l'affichage d'une image selon un Bool contenue dans une de mes classes métiers. J'ai cherche sur internet mais je n'ai pas trouvé comment faire.
    voici mon xaml
    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
     
    <Window x:Class="Wpf.GestionTache.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Wpf.GestionTache"
            mc:Ignorable="d"
            Title="MainWindow" Height="237" Width="525" Loaded="Window_Loaded">
        <Grid Margin="0,0,0,16">
            <Label x:Name="lblName" Content="Gestionnaire de tâches" HorizontalAlignment="Left" Margin="191,10,0,0" VerticalAlignment="Top"/>
            <ListView x:Name="lvTaches" HorizontalAlignment="Left" Height="153" VerticalAlignment="Top" Width="334" Margin="10,36,0,0">
                <ListView.View>
                    <GridView>
                        <GridViewColumn x:Name="gvcTitre" Header="Tâches" DisplayMemberBinding="{Binding titre}"/>
                        <GridViewColumn x:Name="gvcAlarme" Header="Alarme">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Image  Width="16" Height="16" Source="images/reveil.jpg"/>
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
                <Button x:Name="btnAjoutTache" Content="Ajoutée une tâche" HorizontalAlignment="Left" VerticalAlignment="Top" Width="158" Margin="349,36,0,0" Height="27" Click="btnAjoutTache_Click"/>
            <Button x:Name="btnAffiTache" Content="Affichée la tâche" HorizontalAlignment="Left" Margin="349,68,0,0" VerticalAlignment="Top" Width="158" Click="btnAffiTache_Click"/>
            <Button x:Name="btnSupTache" Content="Supprimée la tâche" HorizontalAlignment="Left" Margin="349,93,0,0" VerticalAlignment="Top" Width="158" Click="btnSupTache_Click"/>
        </Grid>
    </Window>
    après j'ai une classe "tache" avec dedans un attribut de type bool qui détermine si sur ma "tache" je dois mettre une alarme.
    Je ne sais pas trop comment faire pour conditions l'affichage de mon image dans le xaml.cs

    je vous remercie de votre aide.

  2. #2
    Membre confirmé Avatar de WaterTwelve21
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2015
    Messages
    270
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2015
    Messages : 270
    Points : 461
    Points
    461
    Par défaut
    Bonjour ,

    Si j'ai bien compris , il y a deux manières de faire ( sans doute d'avantage) :

    • soit tu utilise un booleanToVisibilityConverter très facile d'utilisation (Tu trouveras beaucoup d'information à ce sujet en parcourant le web)
    • soit tu bind la Visibility à une propriété de ton modèle

      Dans le xaml :
      Code : Sélectionner tout - Visualiser dans une fenêtre à part
      Visibility="{Binding Path=IsImgVisible}"
      Dans le xaml.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
        public Visibility IsImgVisible
              {
                  get
                  {  
                      if (conditon == true)
                      {
                         return Visibility.Visible;
                      }
                      return Visibility.Collapsed;
                  }
                  set
                  {
                      _isImgVisible = value; OnPropertyChanged("IsImgVisible");  // Si tu utilise l'interface INotifyPropertyChanged
                  }
              }



    Bon courage pour la suite.
    throw new NoSignatureException();

  3. #3
    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
    Question recurrente !!!
    Si l'attribut de type Bool est dans ton class metier ,la solution est toute prête : c'est dans un Data Trigger bindé sur la valeur Booleen que ca se fait ....

  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
    Re

    code exemple :
    1/ code behind.cs de l'hypothetique class data Tache:

    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
     
     
    public class Tache:INotifyPropertyChanged
        {
            private string titre;
     
            public string Titre
            {
                get { return titre; }
                set { titre = value; RaisePropertyChanged("Titre"); }
            }
            private double niveau;
     
            public double Niveau
            {
                get { return niveau; }
                set { niveau = value; RaisePropertyChanged("Niveau"); }
            }
            private bool alarme;
     
            public bool Alarme
            {
                get { return alarme; }
                set { alarme = value; RaisePropertyChanged("Alarme"); }
            }
     
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void RaisePropertyChanged(string propName)
            {
                PropertyChangedEventHandler h = PropertyChanged;
     
                if(h != null)
                    h(this,new PropertyChangedEventArgs(propName));
            }
        }
    code behind .cs du form :

    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
     
     
      public partial class MainWindow : Window
        {
            private ObservableCollection<Tache> taches = null;
            public MainWindow()
            {
                InitializeComponent();
                taches = GenTaches();
                this.DataContext = taches;
            }
            private Random rnd = new Random();
            private ObservableCollection<Tache> GenTaches()
            {
               ObservableCollection<Tache> l = new ObservableCollection<Tache>();
               for (int i = 1; i < 11; i++)
               {
                   double nv = rnd.Next(30,51) ;
                   bool al = nv > 40.0 ? true : false;
                   Tache t = new Tache() { Titre = "tache"+i.ToString(), Niveau =nv, Alarme = al };
                   l.Add(t);
               }
     
     
               return l;
            }
        }
    code xaml du form:
    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
     
    <Window x:Class="WpfApplication2.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">
        <Window.Resources>
            <Style x:Key="styleimage" TargetType="{x:Type Image}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Alarme}" Value="True">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Alarme}" Value="False">
                        <Setter Property="Visibility" Value="Hidden"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Window.Resources>
     
     
        <Grid Margin="0,0,0,16">
            <Label x:Name="lblName" Content="Gestionnaire de tâches" HorizontalAlignment="Left" Margin="191,10,0,0" VerticalAlignment="Top"/>
            <ListView x:Name="lvTaches" HorizontalAlignment="Left" 
                      Height="153" VerticalAlignment="Top" Width="334" Margin="10,36,0,0"
                      ItemsSource="{Binding }">
                <ListView.View>
                    <!--GridViewColumn gvcAlarmeTemoin est ajouté à des fins d'illustration :espion dans les coulisses--> 
                    <GridView>
                        <GridViewColumn x:Name="gvcTitre" Header="Tâches" DisplayMemberBinding="{Binding Titre}"/>
                        <GridViewColumn x:Name="gvcNiveau" Header="Niveau"  DisplayMemberBinding="{Binding Niveau}"/>
                        <GridViewColumn x:Name="gvcAlarmeTemoin" Header="AlarmeTemoin"  DisplayMemberBinding="{Binding Alarme}"/>
                        <GridViewColumn x:Name="gvcAlarme" Header="Alarme"  >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Image Width="16" Height="16" 
                                               Source="images/reveil.jpg" 
                                               Style="{StaticResource styleimage}">
     
                                        </Image> 
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
     
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
            <Button x:Name="btnAjoutTache" Content="Ajoutée une tâche" HorizontalAlignment="Left" VerticalAlignment="Top" Width="158" Margin="349,36,0,0" Height="27"/>
            <Button x:Name="btnAffiTache" Content="Affichée la tâche" HorizontalAlignment="Left" Margin="349,68,0,0" VerticalAlignment="Top" Width="158" />
            <Button x:Name="btnSupTache" Content="Supprimée la tâche" HorizontalAlignment="Left" Margin="349,93,0,0" VerticalAlignment="Top" Width="158"/>
        </Grid>
    </Window>
    bon code....

Discussions similaires

  1. [WPF / WCF] Image pas net
    Par NeoKript dans le forum Windows Presentation Foundation
    Réponses: 0
    Dernier message: 02/06/2010, 13h05
  2. WPF et image
    Par titou45 dans le forum Windows Presentation Foundation
    Réponses: 11
    Dernier message: 05/01/2010, 19h29
  3. wpf Background Image
    Par Agnès22 dans le forum Windows Presentation Foundation
    Réponses: 1
    Dernier message: 24/11/2009, 13h17
  4. [WPF] Ajouter image dans treeview xaml
    Par chris81 dans le forum Windows Presentation Foundation
    Réponses: 5
    Dernier message: 04/02/2008, 10h06
  5. [3.0][WPF][C#] Images vectorielles
    Par psyclik dans le forum Windows Presentation Foundation
    Réponses: 9
    Dernier message: 16/11/2006, 10h21

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