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 Styling TargetNullValue / FallbackValue


Sujet :

Windows Presentation Foundation

  1. #1
    Membre du Club
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Novembre 2008
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : Boutique - Magasin

    Informations forums :
    Inscription : Novembre 2008
    Messages : 54
    Points : 57
    Points
    57
    Par défaut WPF Styling TargetNullValue / FallbackValue
    Bonjour tout le monde,

    Est-ce que quelqu'un sait comment appliquer un style à la valeur du TargetNullValue ou du FallbackValue une GridViewColumn.
    Exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <GridViewDataColumn DataMemberBinding="{Binding Path=LibelleLongPays, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, TargetNullValue=NULL, FallbackValue=ERROR_BIND}" />
    Comment mettre la valeur du "FallbackValue" en Italic et en rouge quand le binding est en erreur (car c'est à cela que la propriété sert) ?
    Comment mettre la valeur du "TargetNullValue" (c'est à dire NULL) en Italic, quand la valeur de la propriété Binder (LibelleLongPays) est à NULL ?

    Récap :
    Si LibelleLongPays = "FRANCE" , le résultat est FRANCE.
    Si LibelleLongPays = NULL , le résultat affiché doir être NULL
    Si le binding LibelleLongPays n'est pas bon , le résultat affiché doir être ERROR_BIND

    J'espère être assez claire
    Merci d'avance.

  2. #2
    Membre du Club
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Novembre 2008
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : Boutique - Magasin

    Informations forums :
    Inscription : Novembre 2008
    Messages : 54
    Points : 57
    Points
    57
    Par défaut
    Salut, est-ce que quelqu'un a au moins un début d'idée ?

  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
    Maitre des Necroples ,que notre séjour chez toi ageable ...!!!
    Bref DataTrigger est ton meilleur ami dans ce cas de figure puisque l'apparence de tes Controls doit dépendre du Binding c.à.d des données........
    Rappel :
    - si une prop bindée à un control recoit la valeur Null (le type data est un type Reference ou Nullable),TargetNullValue prend le dessus
    - si la Source du Binding n'est pas define ,FallBackValue prends le dessus

    le code suivant devrait reponds au souci :

    1/code behind .cs data d'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
     
    namespace WpfBindingFallBack
    {
        public class Pays : INotifyPropertyChanged
        {
            public Pays()
            {
                ID = 0;
                LibelleLongPays = "nothing";
            }
            public Pays(int pid,string lbpays):this()
            {
                ID = pid;
                LibelleLongPays = lbpays;
            } 
            private int m_id;
            public int ID
            {
                get { return m_id; }
                set { m_id = value; OnPropertyChanged("ID"); }
            }
     
            private string  m_libelleLongPays;
     
            public string LibelleLongPays
            {
                get { return m_libelleLongPays; }
                set { m_libelleLongPays = value; OnPropertyChanged("LibelleLongPays"); }
            }
     
     
     
     
     
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
        }
     
        public class ListPays: ObservableCollection<Pays>
        {
            public ListPays()
            {
                Pays[] p = new Pays[]{
                   new Pays(10,"France"),
                   new Pays(11,"USA"),
                   new Pays(0,null),
                   new Pays(12,"Hollande"),
                   new Pays(13,"Allemagne"),
                   new Pays(14,"Italie"),
                   new Pays(15,"Espagne"),
               };
                foreach (var item in p)
                {
                    this.Add(item);
                }
     
            }
        }
     
     
    }
    2/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
     
    <Window x:Class="WpfBindingFallBack.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfBindingFallBack" 
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:ListPays x:Key="src"/>
     
        </Window.Resources>
        <StackPanel  DataContext="{Binding Source={StaticResource src}}">
            <DockPanel>
     
                <Label Margin="5" Content="ID :" Background="Beige" />
                <TextBox 
                   VerticalAlignment="Center" 
                   Text="{Binding  Path=ID}"/>
                <Label Margin="5" Content="LIBELLE :" Background="Beige" />
                <TextBox 
                    VerticalAlignment="Center" 
                    Text="{Binding  Path=LibelleLongPays}"/>
            </DockPanel>
     
     
            <ListBox 
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding }">
                <ListBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type local:Pays}">
                        <StackPanel Orientation="Horizontal" >
                            <!--illustre FallbackValue la prop ID2 n'est pas defini-->
     
                            <TextBlock 
                                x:Name="tbID"
                                Margin="5" 
                                Text="{Binding  Path=ID2,FallbackValue=ERRORBIND_ID }"/>
                            <!--illustre TargetNullValue la prop LibelleLongPays est Null-->
     
                            <TextBlock 
                                Margin="5" x:Name="tbLibelle"
                                FontFamily="Arial" FontStyle="Normal"  Foreground="Black"  
                                Text="{Binding  Path=LibelleLongPays,
                                Mode=OneWay, UpdateSourceTrigger=PropertyChanged,
                                TargetNullValue=please enter a string }"/>
                        </StackPanel>
                        <DataTemplate.Triggers>
                            <DataTrigger  Binding="{Binding ElementName=tbID, Path=Text}" Value="ERRORBIND_ID">
                                <Setter TargetName="tbID" Property="FontFamily" Value="Arial" />
                                <Setter TargetName="tbID" Property="FontStyle" Value="Italic" />
                                <Setter TargetName="tbID" Property="Foreground" Value="Red" />
                            </DataTrigger>
                            <DataTrigger  Binding="{Binding Path=LibelleLongPays}" Value="{x:Null}">
                                <Setter TargetName="tbLibelle" Property="FontFamily" Value="Arial" />
                                <Setter TargetName="tbLibelle" Property="FontStyle" Value="Italic" />
                                <Setter TargetName="tbLibelle" Property="Foreground" Value="Magenta" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Window>
    bon code......

  4. #4
    Membre du Club
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Novembre 2008
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : Boutique - Magasin

    Informations forums :
    Inscription : Novembre 2008
    Messages : 54
    Points : 57
    Points
    57
    Par défaut
    Salut,
    Je content d'avoir eu une réponse. J'ai essayé ta solution sans avoir de résultat positif ni pour FallbackValue, ni pour le TargetNullValue.
    J'ai bien d'inscrit dans la colonne NULL, ou ERROR_BIND, mais pas le style voulu.

    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
     <telerik:GridViewDataColumn  Header="Code Comptable">
                                        <telerik:GridViewDataColumn.CellTemplate>
                                            <DataTemplate DataType="{x:Type model:M_Pays}">
                                                <TextBlock x:Name="TextBlockCodeComptable"
                                                           FontFamily="Arial"
                                                           FontStyle="Normal"
                                                           Foreground="Black"
                                                           Text="{Binding Path=CodeComptableBis, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, FallbackValue=ERROR_BIND, TargetNullValue=NULL}" />
                                                <DataTemplate.Triggers>
                                                    <DataTrigger  Binding="{Binding ElementName=TextBlockCodeComptable, Path=Text}"
                                                                  Value="ERROR_BIND">
                                                        <Setter TargetName="TextBlockCodeComptable"
                                                                Property="FontFamily"
                                                                Value="Arial" />
                                                        <Setter TargetName="TextBlockCodeComptable"
                                                                Property="FontStyle"
                                                                Value="Italic" />
                                                        <Setter TargetName="TextBlockCodeComptable"
                                                                Property="Foreground"
                                                                Value="Red" />
                                                    </DataTrigger>
                                                    <DataTrigger  Binding="{Binding Path=CodeComptable}"
                                                                  Value="{x:Null}">
                                                        <Setter TargetName="TextBlockCodeComptable"
                                                                Property="FontFamily"
                                                                Value="Arial" />
                                                        <Setter TargetName="TextBlockCodeComptable"
                                                                Property="FontStyle"
                                                                Value="Italic" />
                                                        <Setter TargetName="TextBlockCodeComptable"
                                                                Property="Foreground"
                                                                Value="DarkOrange" />
                                                    </DataTrigger>
                                                </DataTemplate.Triggers>
                                            </DataTemplate>
                                        </telerik:GridViewDataColumn.CellTemplate>
                                    </telerik:GridViewDataColumn>

  5. #5
    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
    le code donne est absolument fonctionnel .Tu dois avoir une "punaise" ou bogue ailleurs !!!

    Le voici revu avec un ListView...
    De plus pour illustration j'ai redeclare la prop ID de type int Nullable dans le class Data...
    1/code behind .cs du class data Pays revu:

    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
     
     
    namespace WpfBindingFallBack
    {
        public class Pays : INotifyPropertyChanged
        {
            public Pays()
            {
                ID = 0;
                LibelleLongPays = "nothing";
            }
            public Pays(int? pid,string lbpays):this()
            {
                ID = pid;
                LibelleLongPays = lbpays;
            } 
            private int? m_id; //un entier nullable 
            public int? ID
            {
                get { return m_id; }
                set { m_id = value; OnPropertyChanged("ID"); }
            }
     
            private string  m_libelleLongPays;
     
            public string LibelleLongPays
            {
                get { return m_libelleLongPays; }
                set { m_libelleLongPays = value; OnPropertyChanged("LibelleLongPays"); }
            }
     
     
     
     
     
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
        }
     
        public class ListPays: ObservableCollection<Pays>
        {
            public ListPays()
            {
                Pays[] p = new Pays[]{
                   new Pays(null,"France"),
                   new Pays(11,"USA"),
                   new Pays(0,null),
                   new Pays(12,"Hollande"),
                   new Pays(13,"Allemagne"),
                   new Pays(14,"Italie"),
                   new Pays(15,"Espagne"),
               };
                foreach (var item in p)
                {
                    this.Add(item);
                }
     
            }
        }
     
     
    }
    2/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
     
     
    <Window x:Class="WpfBindingFallBack.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfBindingFallBack" 
            Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <local:ListPays x:Key="src"/>
     
        </Window.Resources>
        <StackPanel DataContext="{Binding Source={StaticResource src}}">
            <TextBox 
                Text="{Binding Path=LibelleLongPays2, FallbackValue=LibelleLongPays}"/>
     
            <ListView ItemsSource="{Binding }">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="ID" >
                            <GridViewColumn.CellTemplate>
                                <!--un TargetNullValue qui renvoi la chaine NULL,car la prop siurce ID  
                                a ete redclare NULLABLE.c.à.d se comorte comme un Referencre-->
                                <DataTemplate DataType="{x:Type local:Pays}">
                                    <TextBlock 
                                        x:Name="tbID"
                                        FontFamily="Arial"
                                        FontStyle="Normal"
                                        Foreground="Black"
                                        Text="{Binding Path=ID,
                                        Mode=OneWay, UpdateSourceTrigger=PropertyChanged, 
                                        FallbackValue=ERROR_BIND, TargetNullValue=NULL}" />
                                    <DataTemplate.Triggers>
                                        <DataTrigger  Binding="{Binding Path=ID}" Value="{x:Null }">
                                            <Setter TargetName="tbID" Property="FontFamily"  Value="Arial" />
                                            <Setter TargetName="tbID" Property="FontStyle"   Value="Italic" />
                                            <Setter TargetName="tbID" Property="Foreground"  Value="Blue" />
                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn> 
                        <GridViewColumn Header="LibelleLongPays">
                        <GridViewColumn.CellTemplate>
                                <!--un FallbackValue ne peut renvoyer car prop source n'est pas defini-->
                                <DataTemplate DataType="{x:Type local:Pays}">
                                    <TextBlock 
                                        x:Name="tbLibelle"
                                        FontFamily="Arial"
                                        FontStyle="Normal"
                                        Foreground="Black"
                                        Text="{Binding Path=LibelleLongPaysBis,
                                        Mode=OneWay, UpdateSourceTrigger=PropertyChanged, 
                                        FallbackValue=ERROR_BIND, TargetNullValue=NULL}" />
                                    <DataTemplate.Triggers>
                                        <DataTrigger  Binding="{Binding ElementName=tbLibelle, Path=Text}"
                                                                  Value="ERROR_BIND">
                                            <Setter TargetName="tbLibelle" Property="FontFamily"  Value="Arial" />
                                            <Setter TargetName="tbLibelle" Property="FontStyle"   Value="Italic" />
                                            <Setter TargetName="tbLibelle" Property="Foreground"  Value="Red" />
                                        </DataTrigger>
                                        <DataTrigger  Binding="{Binding Path=LibelleLongPays}"  Value="{x:Null}">
                                            <Setter TargetName="tbLibelle"  Property="FontFamily" Value="Arial" />
                                            <Setter TargetName="tbLibelle"  Property="FontStyle"  Value="Italic" />
                                            <Setter TargetName="tbLibelle"  Property="Foreground" Value="DarkOrange" />
                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
     
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn> 
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>
    </Window>
    bon code.....

  6. #6
    Membre du Club
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Novembre 2008
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : Boutique - Magasin

    Informations forums :
    Inscription : Novembre 2008
    Messages : 54
    Points : 57
    Points
    57
    Par défaut
    Salut,

    Effectivement avec le même code mais en changeant : <telerik:RadGridView /> par une <ListView /> ça marche impecablement bien. Je vais voir avec Télérik pourquoi cela ne fonctionne pas.

    Merci beaucoup pour ton aide.

    PS : à 03h15 les gens dorme en général, tu es un oiseau de nuit ?

  7. #7
    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

    hanubis37
    PS : à 03h15 les gens dorme en général, tu es un oiseau de nuit ?
    Chez nous il fait du 40 degres (et 30 degres de nuit) ..
    Cela est du ,au debut de l'ete ,au vent dessechant denomme ,Sirroco, du Sahara...Il fait murir tout ,meme les idees , et empeche le sommeil...
    Que peut un lapin en son gite,à moins de songer !!!

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

Discussions similaires

  1. WPF - Styles & Ressources
    Par hanubis37 dans le forum C#
    Réponses: 2
    Dernier message: 05/06/2013, 18h38
  2. [Débutant] Wpf Style
    Par BenoitM dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 07/02/2013, 10h32
  3. [C#] WPF, Style, Trigger, Binding
    Par dummygreg dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 16/12/2010, 17h51
  4. [WPF] Style TabControl
    Par NeoKript dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 05/06/2010, 16h48
  5. [WPF] Style et snon utilisation dans differents fichiers
    Par escafr dans le forum Windows Presentation Foundation
    Réponses: 1
    Dernier message: 10/02/2010, 09h48

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