How to bind custom object data to a ComboBox
-
I have a custom class Contact. I am trying to bind a List<Contact> to a ComboBox. But I can't get the right syntax/commands for the Windows.Resources part, e.g. the code below gives the error "The type reference cannot find a public type named 'List'", what do I need to fix in Windows.Resources to get this to work? My XAML: <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> My code behind class: 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; } } }
-
Answer:
your problem is in this line ObjectType="{x:Type system:List}" this needs to be the object in which GetContacts is defined. if its in youe window1.xaml.cs it would looks something like this ObjectType="{x:Type X:Window1}" HTH Eric,
Edward Tanguay at Stack Overflow Visit the source
Related Q & A:
- How to display Android database data in a ListView?Best solution by Stack Overflow
- How to save jQuery object in a cookie?Best solution by Stack Overflow
- How to move an object in a circular path?Best solution by Game Development
- How to get custom firmware for a PSP Slim?Best solution by Yahoo! Answers
- How much is 1MB of data on a cell phone?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.