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 :

binding avec plusieurs champs


Sujet :

Windows Presentation Foundation

  1. #1
    Membre régulier Avatar de KRis
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    232
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Avril 2002
    Messages : 232
    Points : 105
    Points
    105
    Par défaut binding avec plusieurs champs
    Bonjour à tous,
    nouveau en WPF, j'espère que je serai suffisamment clair, voici ce que j'essaie de faire :
    j'ai 6 togglebuttons (tgb1,tgb2,tgb3,tgb4,tgb5,tgb6) dans mon appli que je souhaite lié à 2 champs de ma base de données (idPort1, idPort2).
    les valeurs d'idPort1 et idPort2 sont comprises entre 1 et 6.
    si idPort1 = 1, alors tgb1 sera activé, et si idPort2=4, alors tbg4 sera activé.
    Ainsi de suite, on ne peut avoir que 2 togglebuttons activés en même temps.

    Utilisant le binding, je me demande si un togglebutton peut être bindé sur plusieurs champs, et comment coder le converter, s'il en faut un dans ce cas, ce que je crois ?

    Toute idée est la bienvenue, merci par avance !

    Christophe

  2. #2
    Membre expert
    Avatar de Pongten
    Homme Profil pro
    IT Analyst & Software Developer
    Inscrit en
    Juin 2002
    Messages
    1 173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : IT Analyst & Software Developer
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Juin 2002
    Messages : 1 173
    Points : 3 543
    Points
    3 543
    Par défaut
    Bonjour,

    Tu peux déjà regarder du côté du MultiBinding qui permet de combiner plusieurs sources via un converter.

    Il faudra donc, de fait, un converter !

    Peut-être qu'en combinant le MultiBinding avec un DataTrigger, ça peut le faire

  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 442
    Points
    4 442
    Par défaut
    Bonjour KRis....
    Pour des togglebutton "exclusives" as-tu pense à des RadioButtons(ancestor togglebutton) ?
    Des RadioButtons ayant le meme GroupName sont "exclusives"......

    Dans le cas ou les RadioButtons sont bindes à une seule insrance du class Port les RadioButtons se lient facilement via un converter BoolToInt
    (voir 1er exemple ci-apres)

    Dans le cas ou tu aurais en base plusieurs records (id1,id2) il te faut un List<Of Port>.
    Dans ce cas les RadioButtons ne peuvent etre bindes qu'avec le "currentItem" d'un eventuel ItemsControl auquel serait lie notre List<Of Port>(voir 2eme exemple ci-apres)....


    code behind .cs du class Converter commun aux 2 exemples:
    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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Data;
    using System.Globalization;
     
    namespace WpfToggleButton
    {
        public class BooleanToIntValueConverter : IValueConverter
        {
     
            #region IValueConverter Membres
     
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (System.Convert.ToInt32(value).Equals(System.Convert.ToInt32(parameter)))
                {
                    return true;
     
                }
                return false;
            }
     
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (System.Convert.ToBoolean(value))
                {
                    return parameter;
                }
     
                return null;
            }
     
            #endregion
        }
    }
    code behind .cs du class Port commun aux 2 exemples:
    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
    91
    92
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
     
    namespace WpfToggleButton
    {
        public class Port : INotifyPropertyChanged
        {
            public Port( )
            {
            }
            private string _Name1;
            public string Name1 
            {
                get { return _Name1; }
                set 
                {  _Name1=value ;
                    OnPropertyChanged("Name1");
                } 
            }
            private int _ID1;
            public int ID1
            {
                get { return _ID1; }
                set
                {
                    _ID1 = value;
                    OnPropertyChanged("ID1");
                }
            }
            private string _Name2;
            public string Name2
            {
                get { return _Name2; }
                set
                {
                    _Name2 = value;
                    OnPropertyChanged("Name2");
                }
            }
            private int _ID2;
            public int ID2
            {
                get { return _ID2; }
                set
                {
                    _ID2 = value;
                    OnPropertyChanged("ID2");
                }
            }
     
            #region INotifyPropertyChanged Membres
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string nameProperty)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(nameProperty));
     
                }
            }
            #endregion
     
        }
     
        public class ListPorts : ObservableCollection<Port>
        {
            Port p;
            Random rd;
            int num1;
            int num2;
            public ListPorts()
            {
                rd = new Random();
                for (int i = 0; i < 3; i++)
                {
                    num1 = rd.Next(1, 3);
                    num2 = rd.Next(4, 6);
                    p = new Port()
                    {Name1="Port"+(i+1).ToString(),ID1=num1, Name2="Port"+(i+4).ToString(),ID2=num2};
                    this.Add(p);
                }
     
            }
     
        }
    }
    exemple 1
    xaml du winform :
    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
     
    <Window x:Class="WpfToggleButton.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfToggleButton"
            Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <local:BooleanToStringValueConverter x:Key="BooleanToIntValueConverter" />
        </Window.Resources>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <!--les RadionButton "Exclusives" dans le meme GroupName::Group1-->
            <StackPanel 
                Grid.Row="0"
                Grid.Column="0"
                Name="StackPanel1"
                Background="Aqua">
                <RadioButton 
                    Name="RadioButton1"   
                    GroupName="Group1"       
                    Content="ID1"        
                    IsChecked="{Binding Path=ID1, 
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=1}" />
                <RadioButton
                    Name="RadioButton2"                  
                    GroupName="Group1"                   
                    Content="ID2"                    
                    IsChecked="{Binding Path=ID1, 
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=2}" />
                <RadioButton 
                    Name="RadioButton3"       
                    GroupName="Group1"        
                    Content="ID3"           
                    IsChecked="{Binding Path=ID1,
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=3}" />
            </StackPanel>
            <!--les RadionButton "Exclusives" dans le meme GroupName:Group2-->
            <StackPanel 
                Grid.Row="0"
                Grid.Column="1"
                Name="StackPanel2"
                Background="Yellow">
                <RadioButton 
                    Name="RadioButton4"   
                    GroupName="Group2"       
                    Content="ID4"        
                    IsChecked="{Binding Path=ID2, 
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=4}" />
                <RadioButton
                    Name="RadioButton5"                  
                    GroupName="Group2"                   
                    Content="ID5"                    
                    IsChecked="{Binding Path=ID2, 
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=5}" />
                <RadioButton 
                    Name="RadioButton6"       
                    GroupName="Group2"        
                    Content="ID6"           
                    IsChecked="{Binding Path=ID2,
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=6}" />
            </StackPanel>
        </Grid>
    </Window>
    code behind du winform :
    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
     
    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;
     
    namespace WpfToggleButton
    {
        /// <summary>
        /// Logique d'interaction pour Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                DataContext = new Port() { Name1 = "Port1", ID1 = 1, Name2 = "Port1", ID2 = 5 };
            }
        }
    }
    exemple 2
    xaml du winform :
    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
    91
    92
    93
    94
    95
    96
    97
    98
     
    <Window x:Class="WpfToggleButton.Window2"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfToggleButton"
            Title="Window2" Height="300" Width="300">
        <Window.Resources>
            <local:ListPorts x:Key="MyDataSource"></local:ListPorts>
            <local:BooleanToStringValueConverter x:Key="BooleanToIntValueConverter" />
        </Window.Resources>
        <Grid DataContext="{Binding Source={StaticResource MyDataSource},Path=. }">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <StackPanel 
                Grid.Row="0" 
                Grid.Column="0"
                Name="StackPanel1"
                Background="Aqua">
                <RadioButton 
                    Name="RadioButton1"   
                    GroupName="Group1"       
                    Content="ID1"        
                    IsChecked="{Binding Path=ID1, 
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=1}" />
                <RadioButton
                    Name="RadioButton2"                  
                    GroupName="Group1"                   
                    Content="ID2"                    
                    IsChecked="{Binding Path=ID1, 
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=2}" />
                <RadioButton 
                    Name="RadioButton3"       
                    GroupName="Group1"        
                    Content="ID3"           
                    IsChecked="{Binding Path=ID1,
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=3}" />
            </StackPanel>
            <StackPanel 
                Grid.Row="0" 
                Grid.Column="1"
                Name="StackPanel2" 
                Background="Yellow" >
                <RadioButton 
                    Name="RadioButton4"   
                    GroupName="Group2"       
                    Content="ID4"        
                    IsChecked="{Binding Path=ID2, 
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=4}" />
                <RadioButton
                    Name="RadioButton5"                  
                    GroupName="Group2"                   
                    Content="ID5"                    
                    IsChecked="{Binding Path=ID2, 
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=5}" />
                <RadioButton 
                    Name="RadioButton6"       
                    GroupName="Group2"        
                    Content="ID6"           
                    IsChecked="{Binding Path=ID2,
                    Converter={StaticResource BooleanToIntValueConverter},
                    ConverterParameter=6}" />
            </StackPanel>
            <ListBox 
                Grid.Row="1" 
                Grid.ColumnSpan="2"
                BorderBrush="DarkBlue"
                BorderThickness="5"
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding}">
                <ListBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type local:Port}">
                        <StackPanel 
                            Orientation="Horizontal" >
                            <TextBlock
                                Margin="10" Text="{Binding Name1}"/>
                            <TextBlock 
                                Margin="10" Text="{Binding ID1}"/>
                            <TextBlock 
                                Margin="10"  Text="{Binding Name2}"/>
                            <TextBlock 
                                Margin="10"  Text="{Binding ID2}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Window>
    pas de code behind .cs poir ce winform...............

    bon code................................

Discussions similaires

  1. Tableau avec plusieurs champs
    Par lido dans le forum Forms
    Réponses: 9
    Dernier message: 04/02/2008, 12h10
  2. [Tableaux] avec plusieurs champs Parcourir
    Par Camille.CWS dans le forum Langage
    Réponses: 9
    Dernier message: 27/02/2007, 11h29
  3. [Conception] Listes chainées avec plusieurs champs
    Par Nasky dans le forum Général Java
    Réponses: 6
    Dernier message: 11/03/2006, 23h52
  4. Réponses: 3
    Dernier message: 19/11/2004, 21h48
  5. liste deroulante avec plusieurs champs
    Par JulienT dans le forum Struts 1
    Réponses: 4
    Dernier message: 20/04/2004, 17h17

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