Bonjour pour l'instant je suis relativement novice en ce qui concerne WPF.
Je
développe un logiciel de gestion avec MySQL. Et je voudrais que le
chaque control (ComboBox, ListView, ...) est un DataContext différent
issu d'une requête SQL différente.
Je recupère le resultat de mes requêtes SQL dans un DataTable de la manière suivante :
1 | try |
2 | { |
3 | DataTable contentsTable0 = new DataTable(); |
4 | DataTable contentsTable1 = new DataTable(); |
5 | DataTable contentsTable2 = new DataTable(); |
6 | |
7 | //Set the connection String |
8 | String connString = "server=127.0.0.1;uid=root;pwd=;database=maDB;"; |
9 | |
10 | //Set the query |
11 | String query = "SELECT Types.Libelle AS TLib, Marques.Libelle AS MLib, Produits.Nom AS PNom FROM Produits INNER JOIN Types ON Produits.Type=Types.ID INNER JOIN Marques ON Produits.Marque=Marques.ID ORDER BY Types.Libelle, Marques.Libelle, Produits.Nom ASC"; |
12 | |
13 | // Fill the Set with the data |
14 | Connection = new MySqlConnection(connString); |
15 | //Passing the query and connection String |
16 | MySqlDataAdapter da = new MySqlDataAdapter(query, Connection); |
17 | da.Fill(contentsTable0); |
18 | DataContext = contentsTable0; |
19 | query = "SELECT FournisseurPro.Nom AS FournProNom From FournisseurPro"; |
20 | da = new MySqlDataAdapter(query, Connection); |
21 | da.Fill(contentsTable1); |
22 | //CombFournPro.DataContext = contentsTable1; |
23 | query = "SELECT FournisseurClientParticulier.Nom AS FournPartNom From FournisseurClientParticulier"; |
24 | da = new MySqlDataAdapter(query, Connection); |
25 | da.Fill(contentsTable2); |
26 | //CombFournPart.DataContext = contentsTable2; |
27 | } |
28 | catch (MySqlException Ex) |
29 | { |
30 | MessageBox.Show("Erreur SQL :\n" + Ex.Message, "Erreur"); |
31 | } |
CombFournPro
et CombFournPart sont des ComboBox et lorsque je décommente les lignes
22 ou 26 j'ai une exception du type XamlParserException.
Alors que si je change le DataContext racine en faisant :
DataContext = contentsTable2; |
Je n'est aucun problème et cela fonctionne. Et dans mon Xaml j'ai :
1 | <Grid Grid.Row="1"> |
2 | <Grid.RowDefinitions> |
3 | <RowDefinition Height="Auto" /> |
4 | <RowDefinition Height="Auto" /> |
5 | </Grid.RowDefinitions> |
6 | <ComboBox Grid.Row="0" x:Name="CombFournPart" ItemsSource="{Binding}" DisplayMemberPath="FournPartNom"></ComboBox> |
7 | <ComboBox Grid.Row="1" x:Name="CombFournPro" ItemsSource="{Binding}" DisplayMemberPath="FournProNom"></ComboBox> |
8 | </Grid> |
La question est :
Pourquoi je n'arrive pas à changé le DataContext d'un control alors que l'affectation fonctionne avec le DataContext racine ?
Merci