GetClickedElement(Of ListBoxItem) in RadPanelBar

4 posts, 0 answers
  1. Lukas
    Lukas avatar
    42 posts
    Member since:
    Feb 2009

    Posted 27 May 2010 Link to this post


    I try to get the ContextMenu.GetClickedElement() Element when clicking on a ListBoxItem, whose ListBox is nested in a RadPanelBarItem.
                        <telerikN:RadContextMenu x:Name="contextMenu" ItemClick="contextMenu_ItemClick" Opened="contextMenu_Opened"
                            <telerikN:RadMenuItem Header="Löschen" x:Name="delete"/> 
                    <telerikN:RadPanelBar Name="rpb_Vorlagen" HorizontalAlignment="Stretch" Width="505"
                        <telerikN:RadPanelBarItem Header="Administrative Vorlagen" IsExpanded="True" FontWeight="ExtraBold" FontSize="14"
                            <ListBox HorizontalAlignment="Left" Width="498" MaxHeight="215"  Name="lbx_TemplatesAdmin" VerticalAlignment="Top" Background="{StaticResource ControlBrush}"
                                        <RadioButton GroupName="grp1"  Content="{Binding Name}" Checked="CheckBox_Checked" IsChecked="{Binding isChecked,Mode=TwoWay}" FontWeight="Normal" FontSize="12" />                         

    I want to get the DataContext of the ListBoxItem clicked. For that I use this Code:

    Private Sub contextMenu_Opened(ByVal sender As ObjectByVal e As System.Windows.RoutedEventArgs) 
            Dim buf = Me.contextMenu.GetClickedElement(Of ListBoxItem)() 
            If buf IsNot Nothing Then _selectedItem = TryCast(buf.DataContext, DPTemplate) 
            If _selectedItem Is Nothing Then 
                contextMenuTemplate.IsOpen = False 
            End If 
        End Sub 

    But Me.contextMenuTemplate.GetClickedElement(Of ListBoxItem)() always returns nothing!
    If I try RadPanelBarItem instead of ListBoxItem for the Type in GetClickedElement i get the ListBox but not the ListBoxItem.

    Am I missing something?
    How can I retrieve the ListBoxItem clicked in the contextMenue_Open event?

    thanks in advance

  2. Hristo
    Hristo avatar
    832 posts

    Posted 31 May 2010 Link to this post

    Hello Lukas,

    Could you let us know which Silverlight are you using 3 or 4? Also is there browser zoom?
    And let us know which version of the RadControls are you using?
    I've created example using your xaml, SL4 and RadControls for Silverlight 4 (2010.1.422.1040) and its working. It is attached.

    Let us know if your case is different.

    All the best,
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft R3 2016 release webinar banner
  4. Lukas
    Lukas avatar
    42 posts
    Member since:
    Feb 2009

    Posted 31 May 2010 Link to this post

    Hello Hristo ,

    thanks for the answer. I looked at your example and compared it to my project.

    After a while I found that the only major difference ist the type of added Items.

    I have a list of selfwritten class "DPTemplate" and that List is attached to the ItemsSource-Property od the ListBox.
    I thought these Items would be added with Type "ListBoxItem" and as DataContext the DPTemplate-Class.

    But the added items are of type "Object" (says debugger during runtime). In previous version of my project I used to get all Elements under the cursor and loop through them while TryCasting every found element to  type "DPTemplate", but this doesn't work anymore in the new release of TelerikControls.
    (exactly: it still works, but I always have to right-click twice; seems to be due to the new RightClick-Handling in SL 4.0)
    So I want to use the new way, but I cannot use GetClickedElement(Of DPTemplate) because DPTemplate does not inherit FrameworkElement.
    Could you suggest any way to get these items and cast them to type "DPTemplate" without looping trough all element?
    If not I will return to the old way.

    Anyway thanks alot so far,

  5. Lukas
    Lukas avatar
    42 posts
    Member since:
    Feb 2009

    Posted 31 May 2010 Link to this post

    I now found a solution.

    I'm still looping through all elements to find the one of the right type.
    But due to the contextmenue.opened event is fired before the MouseRightButtonUp event is fired I have to suppred the contextmenue event and fire event manually after the MouseRightButtonU is finished.

        Private Sub OnRightMouseButtonUp(ByVal sender As ObjectByVal e As Windows.Input.MouseButtonEventArgs) Handles Me.MouseRightButtonUp 
            Dim mousePosition As Point = e.GetPosition(Nothing
            For Each item As UIElement In VisualTreeHelper.FindElementsInHostCoordinates(mousePosition, Me
                Dim buf = TryCast(item, System.Windows.Controls.ListBoxItem) 
                If buf IsNot Nothing Then 
                    _selectedItem = CType(buf.DataContext, DPTemplate) 
                    contextMenuTemplate.IsOpen = True 
                    Exit For 
                End If 
        End Sub 
        Private Sub contextMenu_Opened(ByVal sender As ObjectByVal e As System.Windows.RoutedEventArgs)  Handles contextMenuTemplate.Opened 
            If _selectedItem Is Nothing Then 
                contextMenuTemplate.IsOpen = False 
            End If 
        End Sub 


Back to Top