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 :

ListBox, ObservableCollection, Binding TextBox Mode=TwoWay ne fonctionne pas [Débutant]


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Septembre 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Finance

    Informations forums :
    Inscription : Septembre 2011
    Messages : 10
    Par défaut ListBox, ObservableCollection, Binding TextBox Mode=TwoWay ne fonctionne pas
    Bonjour

    Je travaille dans .Net C # WPF et je veux utiliser une ListBox avec un DataTemplate et ObservableCollection
    mais je ne peux pas faire fonctionner le Binding du TextBox en Mode TwoWay, quand je modifie le texte dans la TextBox , ObservableCollection n'est

    pas modifiée
    Pouvez-vous m'aider? Merci d'avance

    Je mets ici un exemple simplifié:


    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            WindowStartupLocation="CenterScreen"
            Title="MainWindow" Height="350" Width="525">
     
        <Window.Resources>
            <DataTemplate x:Key="DataTemplate1">
                <Grid Height="75" Width="650">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="25"/>
                        <RowDefinition Height="25"/>
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="0">
                        <TextBox x:Name="TextBox1"
                        Width="400" HorizontalAlignment="Left"
                        Text="{Binding Comment1, Mode=TwoWay,UpdateSourceTrigger=LostFocus}"
                        ScrollViewer.VerticalScrollBarVisibility="Visible"
                        ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                        </TextBox>
                    </Grid>
                    <Grid Grid.Row="1">
                        <TextBox x:Name="TextBox2"
                        Width="400" HorizontalAlignment="Left"
                        Text="{Binding Comment2, Mode=TwoWay,UpdateSourceTrigger=LostFocus}"
                        ScrollViewer.VerticalScrollBarVisibility="Visible"
                        ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                        </TextBox>
                    </Grid>
                </Grid>
            </DataTemplate>
        </Window.Resources>
     
        <DockPanel>
            <Grid DockPanel.Dock="Top" Height="50" >
                <Button Content="Add Item" Height="23" HorizontalAlignment="Left" Name="AddItem1" VerticalAlignment="Center"
                        Width="75" Margin="20,0,0,0" Click="AddItem1_Click"/>
                <Button Content="Listing" Height="23" HorizontalAlignment="Left" Name="Listing1" VerticalAlignment="Center"
                        Width="75" Margin="150,0,0,0" Click="Listing1_Click"/>
            </Grid>
            <ScrollViewer HorizontalScrollBarVisibility="Auto"
                          VerticalScrollBarVisibility="Disabled">
                <Grid HorizontalAlignment="Left" Width="500">
                    <ListBox x:Name="ListBox1" ItemTemplate="{StaticResource DataTemplate1}" ItemsSource="{Binding}"
                        HorizontalAlignment="Left"
                        BorderBrush="Transparent"
                        Background="Transparent"
                        ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                    </ListBox>
                </Grid>
            </ScrollViewer>
        </DockPanel>
    </Window>

    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
        public partial class MainWindow : Window
        {
            ObservableCollection<structInfos> OCInfos;
     
            public struct structInfos
            {
                public string Comment1 { get; set; }
                public string Comment2 { get; set; }
            }
     
            public structInfos _structInfos;
     
            public MainWindow()
            {
                InitializeComponent();
     
                OCInfos = new ObservableCollection<structInfos>();
     
                DataContext = OCInfos;
            }
     
            private void AddItem1_Click(object sender, RoutedEventArgs e)
            {
                _structInfos.Comment1 = "My Text1";
                _structInfos.Comment2 = "My Text2";
     
                OCInfos.Add(_structInfos);
            }
     
            private void Listing1_Click(object sender, RoutedEventArgs e)
            {
                int nCount = OCInfos.Count;
     
                for (int Ind1 = 0; Ind1 < nCount; Ind1++)
                {
                    _structInfos = OCInfos[Ind1];
     
                    Console.WriteLine("");
                    Console.WriteLine(_structInfos.Comment1);
                    Console.WriteLine(_structInfos.Comment2);
                }
            }
        }

  2. #2
    Membre confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2010
    Messages
    95
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2010
    Messages : 95
    Par défaut
    Lorsque tu assignes un struct à une variable, c'est une copie qui est passé et non pas une référence comme c'est le cas pour une classe. Quand tu passes un struct à un datacontext. Tu manipules une copie. Je te conseille d'utiliser une classe.

    Ensuite, une observablecollection ne notifie en rien le changement d'un des objets qu'il contient. Il notifie les changements de la collection. Un objet à été ajouté. Un objet à été supprimé. Un objet à été déplacé. Il ne se charge pas de dire un objet de ma collection a été modifié.

    Pour que tes bindings soit correctement mis à jours. Je t'invite à regarder du coté de l'interface INotifyPropertyChanged.

  3. #3
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Septembre 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Finance

    Informations forums :
    Inscription : Septembre 2011
    Messages : 10
    Par défaut
    Merci beaucoup Deesmon , vous m'avez mis sur la voie et après d'autres recherches, j'ai ainsi résolu mon problème
    Je fournis mon exemple modifié pour que ceux qui comme moi débutent dans le Binding en mode "TwoWay" puissent en profiter

    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
     
     
    ...
     
    	<Grid Grid.Row="0">
    		<TextBox x:Name="TextBox1"
    			Width="400" HorizontalAlignment="Left"
    			Text="{Binding Path=Comment1, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
    	</Grid>
    	<Grid Grid.Row="1">
    		<TextBox x:Name="TextBox2"
    			Width="400" HorizontalAlignment="Left"
    			Text="{Binding Path=Comment2, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
    	</Grid>
     
    ...
    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
     
        public class classInfos : INotifyPropertyChanged
        {
            private string _Comment1;
            public string Comment1
            {
                get { return _Comment1; }
                set
                {
                    if (_Comment1 != value)
                    {
                        _Comment1 = value;
                        OnPropertyChanged("Comment1");
                    }
                }
            }
     
            private string _Comment2;
            public string Comment2
            {
                get { return _Comment2; }
                set
                {
                    if (_Comment2 != value)
                    {
                        _Comment2 = value;
                        OnPropertyChanged("Comment2");
                    }
                }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
     
        public partial class MainWindow : Window
        {
            ObservableCollection<classInfos> OCInfos;
     
            public MainWindow()
            {
                InitializeComponent();
     
                OCInfos = new ObservableCollection<classInfos>();
     
                DataContext = OCInfos;
            }
     
            private void AddItem1_Click(object sender, RoutedEventArgs e)
            {
                classInfos classInfos1 = new classInfos();
                classInfos1.Comment1 = "My Text1";
                classInfos1.Comment2 = "My Text2";
     
                OCInfos.Add(classInfos1);
            }
     
            private void Listing1_Click(object sender, RoutedEventArgs e)
            {
                int nCount = OCInfos.Count;
     
                for (int Ind1 = 0; Ind1 < nCount; Ind1++)
                {
                    Console.WriteLine("");
                    Console.WriteLine(OCInfos[Ind1].Comment1);
                    Console.WriteLine(OCInfos[Ind1].Comment2);
                }
            }
        }

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

Discussions similaires

  1. Le mode debug ne fonctionne pas
    Par gitos dans le forum Eclipse
    Réponses: 2
    Dernier message: 04/01/2010, 18h54
  2. Le binding des mes SetAccelerator ne fonctionne pas
    Par k-koo dans le forum Composants
    Réponses: 3
    Dernier message: 22/10/2009, 09h39
  3. Le mode debug ne fonctionne pas
    Par onimougwo dans le forum Websphere
    Réponses: 4
    Dernier message: 03/04/2008, 14h25
  4. Réponses: 4
    Dernier message: 11/05/2007, 17h37
  5. [VBA-E] SetFocus sur TextBox dans UserForm_Initialize ne fonctionne pas ?
    Par Jipété dans le forum Macros et VBA Excel
    Réponses: 18
    Dernier message: 27/11/2006, 11h39

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