bonsoir
dans mon programme en XAML je désire faire appel à une collection créée lors du chargement de la fenêtre windows
je sais le faire lorsque je crée cette collection via <window.resources> et en effectuant un binding
par contre comment faire dans le premier cas ; j'ai essayé d'utiliser Elementname mais en vain
donc voici mes deux codes
Code c# : 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 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 System.Collections.ObjectModel; namespace WpfApplication1 { /// <summary> /// Logique d'interaction pour MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } string a; Eleves eleves= new Eleves(); private void Window_Loaded(object sender, RoutedEventArgs e) { eleves.Ajouter("dubois", "Michel"); eleves.Ajouter("Rougeux", "Michel"); a = eleves.Count.ToString(); textBox1.Text = a; } } public class Person { private string _Nom; private string _Prenom; public string Nom { get { return _Nom; } set { _Nom = value; } } public string Prenom { get { return _Prenom; } set { Prenom = value; } } public Person(string a, string b) { _Nom=a;_Prenom=b; } } public class Eleves : ObservableCollection<Person> { public Eleves() :base() { Add(new Person("Dupont", "Pierre")); } public void Ajouter(string a, string b) { Add(new Person(a,b)); } } }
Code xml : 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 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> <Window.Resources> <local:Eleves x:Key="eleve2"/> </Window.Resources> <Grid> <TextBox Height="47" HorizontalAlignment="Left" Margin="239,52,0,0" Name="textBox1" VerticalAlignment="Top" Width="181" /> <ListBox Height="54" HorizontalAlignment="Left" Margin="236,140,0,0" Name="listBox1" VerticalAlignment="Top" Width="178" ItemsSource="{Binding ElementName=eleves}" DisplayMemberPath="Nom"/> </Grid> </Window>
Partager