We have a need to allow users to select multiple values from a dropdown list in the grid. We have successfully created a custom column using Devin's blog post as a reference and we can get the values from the Editor control into the underlying object, but we cannot figure out what to do about the binding property in ResolveEditorControl. For single selection scenarios, we use:
m_Editor.SetBinding(XamComboEditor.SelectedItemProperty, editorBinding)
But that won't work for multi-select. We tried leaving the binding off, but the selected values are only displayed for the first item in the grid (although they appear to be persisted to the underlying data object). Any guidance would be greatly appreciated.
Hi,
So there isn't a really good way to do this.
First, SelectedItems isn't a DepenencyProperty, so you can't bind to it.
Even if it was, your underlying property would have to be a Settable Collection of a matching type ObservableCollection<object> in order for it to even be settable.
If you really needed to do it...your best it is to just rig up some events in your ColumnContent provider, and manually populate the comobBox, when it goes into edit mode. And then when the selection chagnes in the comboBox, manually update your underlying item.
-SteveZ
I can easily do that, since we do that a lot in Win Forms apps, but my primary issue is that I don't really understand from the documentation or any other resource that I have looked at, when to tell that a column is going into edit mode. Getting the data out of the control is trivial and quite obvious (ResolveValueFromEditor), but there doesn't appear to be an analogous ResolveValueForEditor (or whatever the appropriate name was). It would be extremely helpful to have more detailed documentation on the various methods, their execution order, and where it is appropriate to interact with the underlying data and/or editor control.
After reviewing the xamGrid source control and performing some experiments, I discovered that the appropriate place to perform this action is in the ResolveEditorControl override. For anyone interested, rather than databinding a property in the xamComboEditor when multiple values are supported, I parse the editorValue and set the selected flags on the combo as follows:
Dim sValues As String Dim cValues As New StringCollection ' Get the current value sValues = CStr(editorValue) ' If there is a value If Not String.IsNullOrEmpty(sValues) Then ' Build an indexed collection of each value in the string For Each sValue As String In sValues.Split(m_Editor.MultiSelectValueDelimiter) ' Ensure we don't run into issues with duplicates If Not cValues.Contains(sValue) Then cValues.Add(sValue) End If Next End If ' Set or clear each item in the combo as appropriate For Each oItem As ComboEditorItem In m_Editor.Items oItem.IsSelected = cValues.Contains(oItem.Data.ToString) Next
Then, in ResolveFromEditor, it is just a matter of extracting the values from the items:
Dim sbText As New Text.StringBuilder(100) If m_Editor.SelectedItems IsNot Nothing AndAlso m_Editor.SelectedItems.Count <> 0 Then For Each oItem As Object In m_Editor.SelectedItems If sbText.Length <> 0 Then sbText.Append(m_Editor.MultiSelectValueDelimiter) End If sbText.Append(oItem.GetType.GetProperty(oColumn.ValueMemberPath).GetValue(oItem, Nothing)) Next End If Return sbText.ToString
Hopefully this will help someone with a similar issue in the future.