Bonsoir à tous.
Voila, je bloque complétement sur le principe du binding.
ComboBox, TextBox (Box_Recherche) et Bouton dans le MainWindow

Deux TextBox dans une UserControl que je mets dans un dossier "View" et que j'appelle "Information"

J'ajoute "Information" dans mon MainWindows
1 2 3
| <Grid x:Name="Information" Grid.Row="3" Grid.Column="0" Grid.RowSpan="1" Grid.ColumnSpan="12">
<view:Information/>
</Grid> |
Je créé deux autre dossiers
- Models
- ModelView
Dans Models je créé une class que j'appelle "Variable_Information"
Dans ModelView je créé une class que j'appelle "ModelView_Information"
Dans la class "Variable_Information"
1 2 3 4 5
| public class Information
{
public string Information1 { get; set; }
public string Information2 { get; set; }
} |
Dans ma class "ModelView_Information"
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
| // Je n'oublie pas les using
public class ModelView_Information
{
public Variable_Information Variable_Information { get; set; }
public ModelView_Information()
{
string sourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\Utilisateur\Desktop\BDD.xlsx';Extended Properties='excel 8.0;HDR=Yes;IMEX=1'";
try
{
OleDbConnection Connexion = new OleDbConnection(sourceConstr);
Connexion.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Feuil1$] where [" + ComboBox + "]= '" + Box_Recherche + "'", Connexion);
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
Variable_Information.Information1= dr[0].ToString();
Variable_Information.Information2= dr[1].ToString();
}
else
{
}
Connexion.Close();
}
catch
{
MessageBox.Show("Impossible de se connecter à la base de donnée. Veuillez réessayer", "Erreur BDD", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
} |
Dans le code behind de MainWindow je créé un event Click sur le bouton.
1 2 3 4 5 6
| private void Bouton_Recherche_Click(object sender, RoutedEventArgs e)
{
ModelView_Information modelView_information = new ModelView_Information();
Information.DataContext = modelView_information;
} |
Pour finir je fait un binding sur les TextBox dans mon UserControl "Information".
Information1
Text="{Binding Variable_Information.Information1}
Text="{Binding Variable_Information.Information2}
Mes problèmes sont les suivants:
- Je ne récupère pas les valeurs de mes variable et rien ne s'affiche dans UserControl "Information". Sauf si je déclare mes variables dans la class "ModelView_Information"
- Je n'arrive pas à récupérer la valeur de Box_Recherche et du choix ComboBox
- Si je mets ModelView_Information dans le DataContext de mon UserControl "Information", tout s'exécute avant que j'appuie sur le bouton.
Merci pour vos réponses
Partager