Bonjour � tous,
Tout d'abord je tiens a signaler que je suis vraiment novice en MFC... Ce que je souhaite faire c'est de parcourir un arbre construit � partir d'un fichier XAML, et pour chaque Control trouv� dans l'arbre lui associer une methode particuliere. Genre, s'il s'agit d'un bouton, lui ajouter un �v�nement OnClick() par exemple... un TextBox, TextChanged(), etc...
Je parcours parfaitement mon arbre et trouve les controls, en revanche impossible d'ajouter des events aux controls...![]()
LoadXAML qui me permet de lire le fichier XAML et de construire l'arbre et de le parcourir en appelant BrowseXAMLTree():
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 DependencyObject^ XAMLfile::LoadXAML() { System::Collections::IEnumerable^ RootChildren; System::Diagnostics::Debug::WriteLine("LoadXAML()"); //Create stream to read the XAML file StreamReader^ mysr = gcnew StreamReader("page1.xaml"); //Create XAML tree according the XAML file System::Windows::DependencyObject^ rootObject = static_cast<DependencyObject^> (XamlReader::Load(mysr->BaseStream)); //Get the children of the root element within the XAML tree RootChildren = LogicalTreeHelper::GetChildren(rootObject); //Browse the XAML tree XAMLfile::BrowseXAMLTree(rootObject, RootChildren); //return the DependencyObject which will be use to attach to the HWND source return rootObject; }
BrowseXAMLTree() qui parcourt l'arbre et pour chaque control trouv� ajoute une action en appelant AssociateActionToXAMLcontrol():
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 void XAMLfile::BrowseXAMLTree(DependencyObject^ RootObject, System::Collections::IEnumerable^ RootChildren) { bool loop = true; //Get the children of the root RootChildren = LogicalTreeHelper::GetChildren(RootObject); //For all root's children, do... do { System::Collections::IEnumerator^ enumChild = RootChildren->GetEnumerator(); if (enumChild->MoveNext()) { RootObject = static_cast<DependencyObject^>(enumChild->Current); //If the object is a UI control (some problem appear in the case of <control.property> if (RootObject->GetType()->Namespace == "System.Windows.Controls") { System::Diagnostics::Debug::WriteLine(RootObject->GetType()->ToString()); XAMLfile::AssociateActionToXAMLcontrol(RootObject); BrowseXAMLTree(RootObject, RootChildren); } } else { loop = false; } } while (loop); return; }
Maitenant je n'arrive pas a coder la fonction AssociateActionToXAMLcontrol()... Je l'avais d�j� cod� en C# et voici le code. Mais en MFC, j'ai tent� de faire pareil, mais le system d'event semble diff�rent...
Voici en C# le code basique aue j'avais fais:
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 private void AssociateAction(DependencyObject obj) { String objetType = obj.GetType().ToString(); switch (objetType) { case "System.Windows.Controls.Button": System.Diagnostics.Debug.WriteLine("Action ajoutée au bouton"); ButtonXAML = obj as System.Windows.Controls.Button; ButtonXAML.Click += new RoutedEventHandler(Button_Click); break; case "System.Windows.Controls.TextBox": System.Diagnostics.Debug.WriteLine("Action ajoutée au TextBox"); TextBoxXAML = obj as System.Windows.Controls.TextBox; TextBoxXAML.TextChanged += new System.Windows.Controls.TextChangedEventHandler(TextBox_TextChanged); break; default: break; } }
Partager