Bonjour a tous,
Après avoir recherche un peu partout sur internet, je suis sur un probleme que je n'arrive pas a résoudre.
Pourtant, ayant déjà fait ce genre de binding je n'arrive pas a comprendre pourquoi cela ne fonctionne pas...
Alors voila, je fais un projet utilisant le pattern MVVM. Dans un projet simple, je veux avoir une textbox et une listbox. Les deux bindant sur des proprietés dans mon ViewModel.
Voici le XAML (Window.xaml):
Le Window.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
16
17
18
19
20 <Window x:Class="testbinding.View.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <ListBox Margin="12,41,12,12" x:Name="listBox1" ItemsSource="{Binding Path=lstItem}" /> <TextBox Height="23" Margin="12,12,12,0" x:Name="textBox1" Text="{Binding Path=Test}" VerticalAlignment="Top" /> </Grid> </Window>
et enfin le test.cs qui est mon ViewModel :
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 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.Navigation; using System.Windows.Shapes; using testbinding.ViewModel; namespace testbinding.View { public partial class Window1 : Window { public Window1() { InitializeComponent(); DataContext = new test(); } } }
Le Binding sur la textbox fonctionne mais pas sur la listbox ! Je n'arrive vraiment pas a comprendre pourquoi... Quelqu'un peut m'aider s'il vous plait ?
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Collections.ObjectModel; using System.Windows.Forms; using testbinding.View; namespace testbinding.ViewModel { class test : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private string m_test; public string Test { get { return m_test; } set { m_test = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Test")); } } } private ObservableCollection<string> lstTest; public ObservableCollection<string> LstTest { get { return lstTest; } set { lstTest = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("lstItem")); } } } public test() { ObservableCollection<string> tmp = new ObservableCollection<string>(); for (int i = 0; i < 500; ++i) { tmp.Add("test" + i.ToString()); } Test = "OK !"; LstTest = tmp; } } }
Partager