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,
I couldn't quite catch what you're trying to achieve. However if you need SelectedValuePath and you're using lots of XamComboEditors you could extend the XamComboEditor and add these two properties. It should look something like the following:
public class XamComboEditorEx : XamComboEditor { public XamComboEditorEx() : base() { this.SelectionChanged += combo_SelectionChanged; } public string SelectedValuePath { get { return (string)GetValue(SelectedValuePathProperty); } set { SetValue(SelectedValuePathProperty, value); } } public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(XamComboEditorEx), new PropertyMetadata(new PropertyChangedCallback(SelectedValuePath_Changed))); public object SelectedValue { get { return (object)GetValue(SelectedValueProperty); } set { SetValue(SelectedValueProperty, value); } } public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(XamComboEditorEx), null); private static void SelectedValuePath_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { var combo = d as XamComboEditorEx; if (combo != null) { combo.ResetSelectedValue(); } } static void combo_SelectionChanged(object sender, EventArgs e) { var combo = sender as XamComboEditorEx; if (combo != null) combo.ResetSelectedValue(); } private void ResetSelectedValue() { if (this.SelectedItem != null) this.SelectedValue = this.SelectedItem.GetType().GetProperty(this.SelectedValuePath).GetValue(this.SelectedItem, null); } }
HTH,
Konstantin Koynov"] Hi, I couldn't quite catch what you're trying to achieve. However if you need SelectedValuePath and you're using lots of XamComboEditors you could extend the XamComboEditor and add these two properties. It should look something like the following: public class XamComboEditorEx : XamComboEditor { public XamComboEditorEx() : base() { this.SelectionChanged += combo_SelectionChanged; } public string SelectedValuePath { get { return (string)GetValue(SelectedValuePathProperty); } set { SetValue(SelectedValuePathProperty, value); } } public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(XamComboEditorEx), new PropertyMetadata(new PropertyChangedCallback(SelectedValuePath_Changed))); public object SelectedValue { get { return (object)GetValue(SelectedValueProperty); } set { SetValue(SelectedValueProperty, value); } } public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(XamComboEditorEx), null); private static void SelectedValuePath_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { var combo = d as XamComboEditorEx; if (combo != null) { combo.ResetSelectedValue(); } } static void combo_SelectionChanged(object sender, EventArgs e) { var combo = sender as XamComboEditorEx; if (combo != null) combo.ResetSelectedValue(); } private void ResetSelectedValue() { if (this.SelectedItem != null) this.SelectedValue = this.SelectedItem.GetType().GetProperty(this.SelectedValuePath).GetValue(this.SelectedItem, null); } } HTH,
Ok, this is close, but not quite what I'm looking for. This gets me about 1/2 of the way there. Since I want to perform some databinding on the SelectedValue, I also need to be able to cascade any changes to the SelectedValue property to correctly update the SelectedItem property.
Yeah, sorry about the confusion. I'm kind of new to the Silverlight arena and was looking for the way we used to do things in some of the older Infragistics WinForms controls for combo editors. Basically being able to set a DisplayMember and ValueMember property on the ComboEditor, set the DataSource for it and be able to bind the Value property to some field.
Anyway, I'll try adding what you've suggested and see if that works for me.
As a suggestion, though, since this seems to be a common occurence in business applications and the code doesn't look too bad, maybe you guys would consider including it as part of the standard feature for the XamComboEditor in the Silverlight control suite?