Reputation: 336
I want to convert a small win-form app to WPF App.
I am using linq-sql and below is the dbml file
This is my xaml code file in which I have 1 combobox and other textboxes
<ComboBox Height="23" IsDropDownOpen="False" Margin="107,52,281,0" Name="cbx_contact" VerticalAlignment="Top" />
<Label Height="17" HorizontalAlignment="Left" Margin="25,55,0,0" VerticalAlignment="Top" Width="72">Contact :</Label>
<TextBox Height="23" Margin="107,85,281,0" Name="txt_name" VerticalAlignment="Top" />
<Label Height="23" HorizontalAlignment="Left" Margin="25,89,0,0" Name="label2" VerticalAlignment="Top" Width="72">Name* :</Label>
<TextBox Height="23" Margin="107,118,281,0" Name="txt_cellno" VerticalAlignment="Top" />
<Label Height="23" HorizontalAlignment="Left" Margin="25,121,0,0" Name="label3" VerticalAlignment="Top" Width="72">Cell No.* :</Label>
<TextBox Height="23" Margin="107,0,281,173" Name="txt_add1" VerticalAlignment="Bottom" />
<Label Height="18.025" HorizontalAlignment="Left" Margin="25,0,0,176" Name="label4" VerticalAlignment="Bottom" Width="72">Address1 :</Label>
<TextBox Height="23" Margin="107,0,281,140" Name="txt_add2" VerticalAlignment="Bottom" />
<Label Height="23" HorizontalAlignment="Left" Margin="25,0,0,138" Name="label5" VerticalAlignment="Bottom" Width="72">Address2 :</Label>
<TextBox Height="23" Margin="107,0,281,107" Name="txt_city" VerticalAlignment="Bottom" />
I want to bind the ComboBox to the contact table with display member "Name" and value member "ContactID"
I tried different-different methods but nothing is working for me...
here's the code which gives error
DataClasses1DataContext db = new DataClasses1DataContext();
var sel = from contact in db.Contacts select new { contactid = contact.ContactID, name = contact.Name };
cbx_contact.ItemsSource = sel;
cbx_contact.DisplayMemberPath = "name";
cbx_contact.SelectedValuePath = "contactid";
I am getting the following error : Cannot create instance of 'Contact_form' defined in assembly 'WpfApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'WpfApplication2;component/Contact_form.xaml' Line 1 Position 7.
And please give me links of good examples of binding wpf gridview. listview and combobox
Upvotes: 2
Views: 6133
Reputation: 11091
I don't believe anonymous types are the problem because this article and this article show you can.
I think you have some syntax issue in your xaml.
to test this out I would probably do some simple midieval testing and first remove the code that sets the Itemssource and see if it runs, if it doesn't you know for sure that it's your xaml, if does then it's in your code-behind.
I would recommend creating an object with all the data structures you need and then set the window's datacontext to that object e.g.
public class ContactViewModel
{
public List<Contact> ContactList {get;set;}
public Contact SelectedContact {get;set;}
}
in the constructor of the window do this:
this.DataContext = new ContactViewModel();
and then your xaml combobox could simply be this:
<ComboBox ItemsSource="{Binding ContactList}" SelectedItem="{Binding SelectedContact}"/>
If ContactViewModel implemented INotifyPropertyChanged you could further modify your xaml to do this:
<Label>Contact:</Label>
<TextBox Text="{Binding SelectedContact.Name}"/>
<Label>Cell #:</Label>
<TextBox Text="{Binding SelectedContact.CellNo}"/>
<Label>Address 1:</Label>
<TextBox Text="{Binding SelectedContact.Address1}"/>
<Label>Address 2:</Label>
<TextBox Text="{Binding SelectedContact.Address2}"/>
Upvotes: 2
Reputation: 1380
The anonymous type may be the issue but it shouldn't be while actually binding, as you are atm only setting the ItemsSource.
In your xaml:
<ComboBox ItemsSource="{Binding Path=.}"
DisplayMemberPath="name"
SelectedValuePath="contactid"/>
In your code behind:
cbx_contact.DataContext = sel;
Should work.
Upvotes: 0