Alrighty, I've been beating my head against a wall here for some time trying to figure out how to do something that, to me at least, should be very simple.
I have a XamComboEditor that I want to function as a lookup, for lack of a better term. I need to figure out how I can pass along a foreign key value (seems like there should be a ValueMemberPath property and a Value property to accomplish what I'm after) to the XamComboEditor and get that value to change the SelectedItem and the display text appropriately. Consequently, when the user changes their selection in the XamComboEditor, I need to have that change cascade back to the field I used as the foreign key.
Simplified scenario:
An Customers table with a StateID field that references the States table that also contains a StateID field and a Name.
I want to be able to bind the StateID field from the Customers table to the StateID field in my States list which I would use as the ItemsSource property for the XamComboEditor and have the SelectedItem change appropriately.
My frustration may be due to simply not understanding Silverlight data binding well enough yet, but to date, the only way I have found to accomplish this it to perform a search of my States list to find the appropriate State item and set that as the SelectedItem programmatically. That's not so bad when you only have one XamComboEditor, but as the number of XamComboEditors increases, this is going to quickly become a pain point. I'm wondering if there's a better way for me to do this? Or will I always be stuck programmatically doing things this way for lookups?
Hi Konstantin,
I am binding to the XamCombo editor as below
<custom:XamComboEditorEx
x:Name="cmb_FieldList1" SelectionChanged="cmb_FieldList1_SelectionChanged" SelectedValueX="{Binding diag:PresentationTraceSources.TraceLevel=High,Path=Field}" SelectedValuePath="Urn" DisplayMemberPath="DisplayName" ItemsSource="{Binding Path=DataContext.DisplayFields, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Margin="0,0,3,0" Width="100" IsEditable="True" AutoComplete="True" MaxDropDownHeight="250" />
I have used the code from your zip file. XamComboEditor's ItemsSource property is set and the items are displayed as expected but the binding to the SelectedValue does not seem to work. The setter on the SelectedValue dependency property is not called. The code for binding is as above. I have tried everything thing but to no avail
What do you recommend as a fix?
Thanks
Sean
hi, I have a sample solution for this issue. use SQL Server 2008 sample database AdventureWorks2008. Use table SalesReason to demo this issue. but I can't upload file here.(got 500 internal error).
How to send you the sample zip file?
Hi Benjamin,
if I understood you correctly you say that the XamComboEditor's ItemsSource property is set and the items are displayed as expected but the binding to the SelectedValue does not seem to work, right?
if this is the case my guess is that MyList is populated after the SelectedValue is bound. So you basically first set the SelectedValue and afterwards set the XamComboEditor's ItemsSource which invalidates current selection. If that's not your case you can provide a sample solution I would be able to investigate it further.
Regards,
Hi I tried your solution, but has a problem to display data properly. What I did as below:
1. Xaml:
<My:XamComboEditorEx ItemsSource="{Binding MyList}" IsEditable="True" AllowFiltering="True" AutoComplete="True" DisplayMemberPath="ItemName" SelectedValuePath="ItemID" SelectedValue="{Binding VM.MyObj.ID, Mode=TwoWay}" />
MyList is: public IEnumerable<MyItem> MyList;
Class MyItem{
public int ItemID;
public strinh ItemName;
....
}
2. When data is loaded by WCF Ria Service, MyList is okay for dropdown popup, but the selected item not display. the editable box is empty.
3. I can change the selection from dropdown and save data correctly by VM.
So question is: how to display the data for the selected value in VM?
Thanks.
Thanks Konstantin Koynov. It worked like a charm.
Thanks your support a lot!
Han