2009-02-02 7 views
0

J'ai une classe personnalisée Contact.Comment lier des données d'objet personnalisées à un ComboBox

J'essaye de lier un List<Contact> à un ComboBox.

Mais je ne peux pas obtenir la bonne syntaxe/commandes pour la partie Windows.Resources, par ex. le code ci-dessous donne l'erreur "La référence du type ne peut pas trouver un type public nommé 'Liste'", que dois-je corriger dans Windows.Resources pour que cela fonctionne?

XAML:

<Window x:Class="dpwpf.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" 
    xmlns:system="clr-namespace:System;assembly=mscorlib" 
    xmlns:local="clr-namespace:dpwpf"> 

    <Window.Resources> 
     <ObjectDataProvider 
      x:Key="contacts" 
      MethodName="GetContacts" 
      ObjectType="{x:Type system:List}"> 
      <ObjectDataProvider.MethodParameters> 
       <x:Type TypeName="local:GetContacts"/> 
      </ObjectDataProvider.MethodParameters> 
     </ObjectDataProvider> 
    </Window.Resources> 


    <StackPanel> 
     <StackPanel> 
      <TextBlock Text="Select the contact:"/> 
      <ComboBox ItemsSource="{Binding 
       Source={StaticResource contacts}}"/> 
     </StackPanel> 
    </StackPanel> 
</Window> 

code behind:

namespace dpwpf 
{ 
    class StoreDB 
    { 
     private string connectionString = "App_Data/main.sqlite"; 

     public List<Contact> GetContacts() 
     { 
      SQLiteConnection conn = new SQLiteConnection("Data Source=" + connectionString); 
      SQLiteCommand cmd = conn.CreateCommand(); 

      List<Contact> contacts = new List<Contact>(); 
      try 
      { 
       conn.Open(); 
       cmd.CommandText = String.Format("SELECT * FROM contacts"); 
       SQLiteDataReader reader = cmd.ExecuteReader(); 
       while (reader.Read()) 
       { 
        Contact contact = new Contact(
         Int32.Parse(reader[0].ToString()), 
         reader[1].ToString(), 
         reader[2].ToString() 
        ); 
        contacts.Add(contact); 
       } 
      } 
      finally 
      { 
       conn.Close(); 
      } 

      return contacts; 
     } 
    } 
} 

Répondre

2

Votre problème est dans cette ligne:

ObjectType="{x:Type system:List}" 

Cela doit être l'objet dans lequel GetContacts est défini ned.

Dans votre window1.xaml.cs ce serait ressemble à quelque chose comme ceci:

ObjectType="{x:Type X:Window1}" 
Questions connexes