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

GetClickedElement(Of ListBoxItem) in RadPanelBar

3 Answers 57 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Lukas
Top achievements
Rank 2
Lukas asked on 27 May 2010, 01:07 PM
Hi,

I try to get the ContextMenu.GetClickedElement() Element when clicking on a ListBoxItem, whose ListBox is nested in a RadPanelBarItem.
XAML:
<Canvas> 
                <telerikN:RadContextMenu.ContextMenu> 
                    <telerikN:RadContextMenu x:Name="contextMenu" ItemClick="contextMenu_ItemClick" Opened="contextMenu_Opened"
                        <telerikN:RadMenuItem Header="Löschen" x:Name="delete"/> 
                    </telerikN:RadContextMenu> 
                </telerikN:RadContextMenu.ContextMenu> 
 
 
                <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}"
                            <ListBox.ItemTemplate> 
                                <DataTemplate>                                     
                                    <RadioButton GroupName="grp1"  Content="{Binding Name}" Checked="CheckBox_Checked" IsChecked="{Binding isChecked,Mode=TwoWay}" FontWeight="Normal" FontSize="12" />                         
                                </DataTemplate> 
                            </ListBox.ItemTemplate> 
                        </ListBox> 
                    </telerikN:RadPanelBarItem> 
</telerikN:RadPanelBar> 
 
            </Canvas> 

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

Lukas

3 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 31 May 2010, 09:18 AM
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,
Hristo
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.
0
Lukas
Top achievements
Rank 2
answered on 31 May 2010, 10:46 AM
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,

Lukas
0
Lukas
Top achievements
Rank 2
answered on 31 May 2010, 11:06 AM
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 
        Next 
    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 
 


thx

Lukas
Tags
PanelBar
Asked by
Lukas
Top achievements
Rank 2
Answers by
Hristo
Telerik team
Lukas
Top achievements
Rank 2
Share this question
or