This is a migrated thread and some comments may be shown as answers.

Programmatically set SelectedItem(s)

2 Answers 405 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 14 Feb 2013, 03:11 PM
I am using a RadAutoCompleteBox to list/select an assigned user to an object. The data is unbound and the itemssource is generated by polling an Active Directory tree. When an item is selected, the value is stored in the property of the datacontext as the principal's SID. My problem is that when I later access the object, I cannot set the SelectedItem of the RadAutoCompleteBox to one of the principals in the list and I have to resort to setting the SearchText which still pops up the suggested values when the control receives focus. I would like to be able to set the SelectedItem instead so this does not happen.

In the Page.xaml
<telerik:RadAutoCompleteBox
    x:Name="PrincipalAutoCompleteBox"
    WatermarkContent="Select a principal"
    TextSearchMode="Contains"
    AutoCompleteMode="SuggestAppend"
    SelectionMode="Single"
    TextSearchPath="Name"
    DropDownItemTemplate="{StaticResource ResourceKey=DSPrincipalAutoComplete}"
    DropDownWidth="Auto"
    FilteringBehavior="{StaticResource ResourceKey=CustomFilteringBehaviour}"
    GotFocus="PrincipalAutoCompleteBox_GotFocus"
    KeyDown="PrincipalAutoCompleteBox_KeyDown"
    Populated="PrincipalAutoCompleteBox_Populated"
/>

In the Page.xaml.vb
Private Sub PrincipalAutoCompleteBox_GotFocus(sender As System.Object, e As System.Windows.RoutedEventArgs)
  Dim autoComplete As RadAutoCompleteBox = DirectCast(sender, RadAutoCompleteBox)
  Dim searchString As String = autoComplete.SearchText
  If autoComplete.SelectedItem IsNot Nothing Then
    searchString = BindingExpressionHelper.GetValue(autoComplete.SelectedItem, autoComplete.DisplayMemberPath).ToString()
  End If
  autoComplete.IsDropDownOpen = False
  autoComplete.Populate(searchString)
End Sub
  
Private lastFilteredItems As IEnumerable(Of Object)
Private Sub PrincipalAutoCompleteBox_KeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs)
  Dim autoCompleteBox As RadAutoCompleteBox = DirectCast(sender, RadAutoCompleteBox)
  If e.Key = Key.Tab AndAlso e.Handled = False AndAlso autoCompleteBox.SelectedItem Is Nothing Then
    If lastFilteredItems.Count = 1 Then
      autoCompleteBox.SelectedItem = lastFilteredItems.OfType(Of Object).FirstOrDefault
    Else
      autoCompleteBox.SearchText = Nothing
    End If
  End If
End Sub
  
Private Sub PrincipalAutoCompleteBox_Populated(sender As System.Object, e As System.EventArgs)
  Me.lastFilteredItems = DirectCast(sender, RadAutoCompleteBox).FilteredItems
End Sub

And the custom filter
Public Class CustomFilteringBehaviour
  Inherits Telerik.Windows.Controls.FilteringBehavior
  
  Public Overrides Function FindMatchingItems(searchText As String, items As System.Collections.IList, escapedItems As System.Collections.Generic.IEnumerable(Of Object), textSearchPath As String, textSearchMode As Telerik.Windows.Controls.TextSearchMode) As System.Collections.Generic.IEnumerable(Of Object)
    If String.IsNullOrEmpty(searchText) Then
      Return items.OfType(Of Object).Where(Function(x) Not escapedItems.Contains(x))
    Else
      Return MyBase.FindMatchingItems(searchText, items, escapedItems, textSearchPath, textSearchMode)
    End If
  End Function
End Class

Finally, the code where I populate the the list and attempted to preselect an item based on the datacontext
'^-- principalList As ObservableCollection(Of DSPrincipal) and then populated from AD
PrincipalAutoCompleteBox.ItemsSource = principalList
For Each principal As DSPrincipal In principalList
  If principal.Sid = DirectCast(Me.DataContext, MyObject).Principal.SID Then
    'v-- What I tried to do but it threw an error
    'PrincipalAutoCompleteBox.SelectedItem = principal
    'v-- What I ended up doing
    PrincipalAutoCompleteBox.SearchText = _principal.Name
    Exit For
  End If
Next

Is it possible to set SelectedItem in some way? Would binding SelectedItem to the property allow it to display automatically when the datacontext changes? What I have works but I would like a consistant experience for my user where the suggested items do not pop up when the value has been set previously.

Thanks,
Jason

2 Answers, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 20 Feb 2013, 02:32 PM
Hi Jason,

I believe you are experiencing an issue we had in RadAutoCompleteBox - when setting a SelectedItem the text portion of the control was not updated. This should be fixed now and you can test it with the latest internal release or wait for the Q1 2013 release - it will be out until the end of the month.

Greetings,
Ivo
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Jason
Top achievements
Rank 1
answered on 20 Feb 2013, 05:48 PM
I was actually getting a thrown error but I will wait until the Q1 2013 release to see if I still have the same problem.
Tags
AutoCompleteBox
Asked by
Jason
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Jason
Top achievements
Rank 1
Share this question
or