-
Oct 21st, 2010, 07:47 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Combobox Databinding Text versus Datasource in WPF
Hi guys,
As anyone who has read any of my other posts now knows, I'm converting a winforms app to wpf and trying to get the same or similar results of the winforms controls in to my wpf app. As I have been finding each answer or have been assisted by the experts in vbforums I've been posting the results so that hopefully other winforms developers can start making the transition to wpf with real world examples and hurdles that have been overcome. Having said that, here's my next question.
I have a data entry form in my app that in winforms had a combobox attached to it where you choose a manufacturing customer type from it and can hit a save button. If you scroll through my list of customers, the text displayed in that combobox is actually pulled from the customer table, not the customer_type table. This is accomplished in winforms by setting the datasource property to customer_type_bindingsource, and the Databindings Text property to customer_bindingsource.
As stated above, the combobox datasource is set to a table in my sql server that has a list of manufacturing types in it. The databinding to the text property of the combobox however, is set to the customer table so the chosen value can be stored there.
In short, how do i set the itemssource to the customer type table, but the item text displayed to the customer table in either xaml or in codebehind for WPF as i scroll through the customers?
Last edited by trevorjeaton; Oct 21st, 2010 at 08:23 AM.
-
Oct 21st, 2010, 08:09 AM
#2
Thread Starter
Hyperactive Member
Re: Combobox Databinding Text versus Datasource in WPF
as per above, here is the code in the class itself:
Code:
Public Class Customers
Private customerdata As New CustomerDataSet 'this dataset contains both the customer table and the customer_type table
...
...
...
End Class
here's the window_loaded:
Code:
Me.DataContext = Me.customerdata.Customer 'set the context of the window to the customer table
Me.cbCustomerType.DataContext = Me.customerdata.Customer_Type 'set the context of the specific combobox to be the type
and finally the xaml for the combo:
Code:
<ComboBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="361,70,0,0" Name="cbCustomerType" VerticalAlignment="Top" Width="300" IsEnabled="False" IsReadOnly="True" DisplayMemberPath="Customer_Type" ItemsSource="{Binding}" Text="{Binding Path=Customer_Type}"/>
Additional info to add would be that the TABLE called 'customer_type' also has a FIELD in it called 'customer_type' as well that i'm trying to display - and last but not least, the customer table ALSO has a 'customer_type' field in it that i'm trying to save/display. I have flags that enable it and remove the read only status as well depending on if somebody clicks an 'edit' button
thanks all and as usual, if i find the solution myself before any of you wpf wizards can help, i'll post it......
-
Oct 21st, 2010, 08:26 AM
#3
Thread Starter
Hyperactive Member
Re: Combobox Databinding Text versus Datasource in WPF
i'm leaning toward the solution being in the xaml where you tell it where it binds, but am just starting to research this, so again, if anybody has come across this before, i'm all ears....
-
Oct 21st, 2010, 10:10 AM
#4
Thread Starter
Hyperactive Member
Re: Combobox Databinding Text versus Datasource in WPF
by the way, removing the datacontext of cbCustomerType and leaving the entire form with the customerdatacontext works just fine with this xaml:
Code:
<ComboBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="361,70,0,0" Name="cbCustomerType" VerticalAlignment="Top" Width="300" IsEnabled="False" IsReadOnly="True" DisplayMemberPath ="Customer_Type" ItemsSource="{Binding}" Text="{Binding Path=Customer_Type}"/>
but when i drop down the combo, i see an exact count of the number of customer types in the customer database for items - ie:
Automotive
Automotive
Automotive
General Manufacturing
General Manufacturing
Apparel
Apparel
etc etc - in this case its reflecting that three of the customers on file in the customers table are automotive, 2 are general manufacturing and 2 are apparel - when i scroll through the list of customers (a listbox) the text displayed in the combo is updated 100% correctly along with all of the other texboxes in the window bound to the view, however, if i choose edit and drop down the combobox, i'm looking for this as the desired result (using the above example - so pulling the list from the customer_type table, which has only one entry for each of course):
Automotive
General Manufacturing
Apparel
-
Oct 22nd, 2010, 10:04 AM
#5
Thread Starter
Hyperactive Member
Re: Combobox Databinding Text versus Datasource in WPF
okay so now i added this code to set the datacontext of the combobox and it displayed the items correctly, however, now the displaymemberpath is set along with the itemssource (as it should be) and is not changing the text to be what is saved in the Customer.Customer_Type field:
here's the code:
Code:
Me.DataContext = Me.customerdata.Customer
Me.cbCustomerType.DataContext = Me.customerdata.Customer_Type
and the new xaml:
Code:
<ComboBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="361,70,0,0" Name="cbCustomerType"
VerticalAlignment="Top" Width="300" IsEnabled="False" IsReadOnly="True"
ItemsSource="{Binding}" DisplayMemberPath ="Customer_Type"
Text="{Binding Path=Customer_Type}" />
so how do i get the itemssource to be Customer_Type.Customer_Type (which it is at the moment), and the DisplayMemberPath and/or Text to be Customer.Customer_Type???? (which it is not at the moment - its Customer_Type.Customer_Type)
of course if i remove the second line in the codebehind, the datacontext of the entire window is set to Customer and the behavior reverses and acts like my first few posts above.....
-
Oct 24th, 2010, 01:49 PM
#6
Thread Starter
Hyperactive Member
Re: Combobox Databinding Text versus Datasource in WPF
okay so this one was fun - gotta love the simple act of getting your terminology right in order to pose the correct questions. The solution here is that I was looking for a WPF Lookup Combobox - absolutely everything is explained here:
http://msdn.microsoft.com/en-us/vbasic/cc788742.aspx
compliments of my hero, Beth Massi.
the code is attached for my solution in particular.....
here is at the class level:
Code:
Private customerdata As New CustomerDataSet
Private customer_type_data As New Customer_TypeDataSet
Private tacust As New CustomerDataSetTableAdapters.CustomerTableAdapter
Private tamanager As New CustomerDataSetTableAdapters.TableAdapterManager
Private Customer_Type_Lookup As New Customer_TypeDataSet.Customer_TypeDataTable
and at the window loaded level:
Code:
Dim tacustomer_type_lookup As New Customer_TypeDataSetTableAdapters.Customer_TypeTableAdapter
tacustomer_type_lookup.Fill(Me.Customer_Type_Lookup)
Me.tacust.Fill(Me.customerdata.Customer)
Me.tamanager.CustomerTableAdapter = tacust
Me.DataContext = Me.customerdata.Customer
Me.cbCustomerType.ItemsSource = Me.Customer_Type_Lookup
and finally the correct xaml:
Code:
<ComboBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="361,70,0,0" Name="cbCustomerType"
VerticalAlignment="Top" Width="300" IsEnabled="False" IsReadOnly="True"
DisplayMemberPath ="Customer_Type"
SelectedValue="{Binding Path=Customer_Type}"
SelectedValuePath="Customer_Type"/>
to my knowledge, the above code will reproduce the exact same thing as a winforms developer setting the datasource of a combobox to one table, then setting the databinding Text property to another. Also, keep in mind that in both my customer table and my customer_type table, the field name was the same, so in your case, if they are different, the xaml would need to reflect the displaymemberpath and the SelectedValuePath as the field name in the lookup table (the winforms datasource) and the SelectedValue would need to be the field name in the Table you are browsing (the winforms databindings text property)
enjoy :-)
Thanks to Beth Massi and her contributions to us VB newbies
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|