GridViewBoundColumnBase AutoCompleteBoxColumn handling events, howto

10 posts, 0 answers
  1. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 03 Oct 2011 Link to this post

    I am trying to implement a autocompletebox in a grid column using GridViewBoundColumnBase

    I don't know how to handle events, I assume it's something like the ???? below

     

     

     

    Public Class AutoCompleteBoxColumn
        Inherits GridViewBoundColumnBase
       
        Public Overloads Overrides Function CreateCellEditElement(ByVal cell As GridViewCell, ByVal dataItem As Object) As FrameworkElement
      
            Me.BindingTarget = AutoCompleteBox.SelectedItemProperty
      
            Dim cellEditElement As AutoCompleteBox = New AutoCompleteBox()
      
            cellEditElement.SetBinding(AutoCompleteBox.SelectedItemProperty, CreateValueBinding())
            ??????cellEditElement.AddHandler(  gridAutoCompleteBox_Populating )
            Return (TryCast(cellEditElement, FrameworkElement))
        End Function
        Private Function CreateValueBinding() As System.Windows.Data.Binding
            Dim valueBinding As New System.Windows.Data.Binding()
            valueBinding.Mode = BindingMode.TwoWay
            valueBinding.NotifyOnValidationError = True
            valueBinding.ValidatesOnExceptions = True
            valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit
            valueBinding.Path = New PropertyPath(Me.DataMemberBinding.Path.Path)
            Return valueBinding
        End Function
    End Class


    Private Sub gridAutoCompleteBox_Populating(sender As System.Object, e As System.Windows.Controls.PopulatingEventArgs)
        'Access database here
        
    e.Cancel = True
    End Sub



    Any ideas
    Thanks
    Jim
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Oct 2011 Link to this post

    Hello Jim,

    I would recommend you to work directly with GridViewComboBoxColumn. Once you set its IsComboBoxEditable property to "True", the autocomplete functionality will be turned on by default.
    Please take a look at our online documentation and demos for a reference.


    Greetings,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 03 Oct 2011 Link to this post

    I need the functionality of the autocomplete box.
    I can get it working OK as a stand alone control.
    I originally tried <DataTemplate> which nearly worked.
    And then GridViewBoundColumnBase which again nearly worked.
    I have difficulty accessing the itemsource and setting handlers once it's in the Grid
    Thanks
    Jim
  4. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Oct 2011 Link to this post

    Hello Jim,

    I am sending you a sample project illustrating how you can define an AutoComplteBox as a CellEditTemplate. You can add handlers directly to the element.
    Will that work for you ? 

    Best wishes,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 03 Oct 2011 Link to this post

    Thanks I look forward to reading this in the morning.
    For now it's time for zzzz
    Regards
    Jim
  6. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 04 Oct 2011 Link to this post

    Hello and thanks for the example , I am getting nearer.
    1) I should have worked out how to capture events by just adding an attribute to the xaml. I was thrown by ...
    Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
    2) I can get which AutoCompleteBox generating the populating event from the sender parameter, and thus set its Item.Source dynamically.

    So my autocompletebox offers the drop down and the cell displays the selected value.
    However when I move to a new row the data reverts to what was originally in the row.
    I have tired below to no avail.
    Jim
    Private Sub gridAutoCompleteBox_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
          Dim selectedRoomType As Roomtype = DirectCast(e.AddedItems(0), Roomtype)
          Dim currentRowRoomType As Roomtype = DirectCast(RadGridView1.SelectedItem, Roomtype)

          RadGridView1.Items.EditItem(currentRowRoomType)

          currentRowRoomType = selectedRoomType
          RadGridView1.Items.CommitEdit()
          Dim x As Integer = 0
      End Sub


  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 04 Oct 2011 Link to this post

    Hello Jim,

    How do you define your column - do you create your own custom one or you used the approach with having a CellEditTemplate ? Do you set the DataMemberBinding of the column ? Will it be possible to update the project I attached previously so that it reflects your scenario and let me know how do you change it  ? 

     

    Kind regards,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  8. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 04 Oct 2011 Link to this post

    Grid xaml is ..
    <
    telerik:RadGridView AutoGenerateColumns="False" Grid.Row="1" HorizontalAlignment="Left" Name="RadGridView1" VerticalAlignment="Top" Grid.ColumnSpan="4" Height="282" Width="640" >
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn UniqueName="RoomTypeCode" DataMemberBinding="{Binding RoomTypeCode}" Header="Room Type Code">
                <telerik:GridViewDataColumn.CellEditTemplate>
                    <DataTemplate>
                        <sdk:AutoCompleteBox  x:Name="jimsAutoCompleteBox" SelectionChanged ="gridAutoCompleteBox_SelectionChanged" Populating="gridAutoCompleteBox_Populating" VerticalAlignment="Top" ValueMemberPath="RoomTypeCode" Width="133" HorizontalAlignment="Left" Grid.ColumnSpan="2" MinimumPrefixLength="1" MinimumPopulateDelay="500">
                            <sdk:AutoCompleteBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding RoomTypeCode}" />
                                        <TextBlock Text=" : " />
                                        <TextBlock Text="{Binding Description}" />
                                    </StackPanel>
                                </DataTemplate>
                            </sdk:AutoCompleteBox.ItemTemplate>
                        </sdk:AutoCompleteBox>
                    </DataTemplate>
                </telerik:GridViewDataColumn.CellEditTemplate>
            </telerik:GridViewDataColumn>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Description, Mode=TwoWay}" Header="Room Description"  Background="#FFEBEBEB" IsReadOnly="True"/>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
  9. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 04 Oct 2011 Link to this post

    Hmm
    Tried your supplied example again.
    And whilst the AutoCompleteBox and drop down seem to work, it appears that the data does not go in the grid when you exit the cell
    Or am I doing something wromg ?
    Jim
  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 05 Oct 2011 Link to this post

    Hi Jim,

    You are quite right. I missed to set just a single property in the CellEditTemplate definition. Please set the Mode property of Text Binding of AutoComplateBox:

    <input:AutoCompleteBox Text="{Binding Name, Mode=TwoWay}"                                                 
                                                       ItemsSource="{Binding Players, Source={StaticResource MyViewModel}}" />

     

    Best wishes,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top