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,
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?
Could you please summarize your requirements just to ensure that we are working on the exact same thing in order to be able to log a feature request for you.
I am looking forward to hearing from you.
Sure, my requirements are really quite simple:
Add a ValueMemberPath and a Value property to the XamComboEditor. These two properties would function the same way they do for the ValueMember and Value properties on the UltraComboEditor. The ValueMember property would be a property in the ItemsSource collection that would hold a "value" while the DisplayMemberPath would hold the text to display when a selection is made or the value is changed.
If I change the SelectedItem or SelectedIndex property of the XamComboEditor, when I query the Value property I would expect it to give the value from the property specified in the ValueMemberPath property contained in the ItemsSource collection.
Consequently, if I set the Value property of the XamComboEditor, I would expect it to automatically update the SelectedItem and SelectedIndex properties based using the property specified in the ValueMemberPath property.
If the above is confusing, I apologize. The simplest way I can explain it is to add functionality to the XamComboEditor so it functions the same way the Value and ValueMember properties do on the UltraComboEditor.
We are currently working on solving your issue and by now I have sent your request about adding the new properties directly to our product management team.
Our product team chooses new feature requests for development based on popular feedback from our customer base. Infragistics continues to monitor application development for all of our products, so as trends appear in requested features, we can plan accordingly.
We value your input, and our philosophy is to enhance our toolset based on customer feedback. If your feature is chosen for development, you will be notified at that time. Your reference number for this feature request is FR13268.
If you would like to follow up on your feature request at a later point, you may contact Developer Support management via email. Please include the reference number of your feature request in the subject and body of your email message. You can reach Developer Support management through the following email address: dsmanager@infragistics.com
Thank you for your request.
here is the code that should work both ways:
public class XamComboEditorEx : XamComboEditor { public XamComboEditorEx() : base() { this.SelectionChanged += combo_SelectionChanged; } #region SelectedValuePath 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))); private static void SelectedValuePath_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { var combo = d as XamComboEditorEx; if (combo != null) { combo.ResetSelectedValue(); } } #endregion //SelectedValuePath #region SelectedValue public object SelectedValue { get { return (object)GetValue(SelectedValueProperty); } set { var q = (from item in Items where item.Data.GetType().GetProperty(SelectedValuePath).GetValue(item.Data, null).Equals(value) select item).FirstOrDefault(); if (q == null) this.SelectedItem = null; else this.SelectedItem = q.Data; } } public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(XamComboEditorEx), null); #endregion //SelectedValue 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.SelectedItem.GetType().GetProperty(this.SelectedValuePath) != null) { var propInfo = this.SelectedItem.GetType().GetProperty(this.SelectedValuePath); this.SetSelectedValueSilently(propInfo.GetValue(this.SelectedItem, null)); } else this.SetSelectedValueSilently(null); } private void SetSelectedValueSilently(object value) { this.SetValue(XamComboEditorEx.SelectedValueProperty, value); } }
Note: if there are multiple items which SelectedValues and you try to set this value to the SelectedValue property - the first one will be selected.
Please let me know if you need any further assitense!
Regards,
Hello Luc,
Currently the best way to achieve the functionality you want is to use the EmptyText. You can also set the SelectedValue Property, but this way the value should exist in the ItemsSource.
hello Stefan !
Thank you for your answer.
Well, I can't wait the future version of XamComboEditor, so do you have another way than Emptytext to give a default value to a XamComboEditor ?
Luc
Currently the cross-platform XamComboEditor doesn't have a ValuePath or Value Property and the approach Konstantin suggested seems to be the best one for having SelecdtedValue. If it doesn't meets all your requirements, I can log a Product Idea on your nehalf for adding a SelectedValue Property to the XamComboEditor in our future versions.
Looking forward for your reply.
Hi !
Sorry, but the function SelectedValue doesn't work at all for me ! I can't get/set a value to my XamComboEditor.
I need to set a default value. I have a XamComboEditor which is binded to a storage procedure(which give me an ID and a Name).
I want to get the ID and display the linked Name in the XamComboEditor as default value.
Example:
Constructor()
{
InitialiseComponent();
myxamcombo.SelectedValue = myobject.ID;
myxamcombo.DisplayValue = myobject.Name;
}
Hello Sean,
It has been a while since you have made your post, in case you still need support I will be glad to assist you further. I suppose the other community members can benefit from this answer as well. I have been looking into your post, but it seems like that I am missing something in your scenario, so if this is still an issue for you, could you please send an isolated sample project, where the issue is reproduced, so I can investigate it further for you.
Feel free to write me if you have further questions.