Trouble putting ContextMenu on ListBoxItems nested in RadPanelBarItem

4 posts, 0 answers
  1. Scott
    Scott avatar
    30 posts
    Member since:
    Aug 2011

    Posted 16 May 2012 Link to this post

    Hi All,

    Edit: The title should state that the issue is with ListView items and not ListBox items. Cannot change it now - sorry about the confusion.

    I am trying to put a context menu on items in a ListView when that ListView is contained within a RadPanelBarItem that has a context menu. I hope the code example below illustrates what I am trying to do. If I don't add the following then the list items show the expected menu.

    <telerik:RadContextMenu.ContextMenu>
         <telerik:RadContextMenu x:Name="_macroActionContextMenu" />
     </telerik:RadContextMenu.ContextMenu>

    When I add it (as shown in the snippet below) then I always get the context menu associated with the RadPanelBarItem when I right click on the ListView elements rather than the context menu specified for the ListView.ItemContainerStyle.

    Any help would be appreciated. My code snippet is below.

            <telerik:RadPanelBarItem Name="_rpbiMacros" Header="Macros" IsDropAllowed="False" Foreground="#FF4D4D4D" BorderBrush="#FFBCB596" >
                <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu x:Name="_macroActionContextMenu" />
                </telerik:RadContextMenu.ContextMenu>
                <!--Constrain the size of the wrap panel so it can do its thing-->
                <Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadPanelBar}}, Path=ActualWidth, Converter={StaticResource mathConverter}, ConverterParameter=@VALUE-12;0}">
     
                    <!-- View as icons -->
     
                    <WrapPanel Name="_wrpMacroActions"
                        Visibility="{Binding ViewMacroActionListAsIcons, Converter={StaticResource boolToVisibilityConverter}, ConverterParameter=Visibility.Collapsed}">
                        <ItemsControl ItemsSource="{Binding MacroActionViewModelList}" >
     
    ... omitted for clarity
     
                        </ItemsControl>
                    </WrapPanel>
     
                    <!-- View as list -->
     
                    <ListView x:Name="_lviewMacroActions" ItemsSource="{Binding MacroActionViewModelList}" ButtonBase.Click="OnClick_ListViewHeader"
                        Visibility="{Binding ViewMacroActionListAsIcons, Converter={StaticResource invertBoolToVisibilityConverter}, ConverterParameter=Visibility.Collapsed}" >
                        <ListView.Resources>
                            <ContextMenu x:Key="itemContextMenu">
                                <MenuItem Header="Open" Click="OnClick_EditMacroAction">
                                </MenuItem>
                                <MenuItem Header="Delete" Click="OnClick_DeleteMacroAction" >
                                    <MenuItem.Icon>
                                        <ContentControl Template="{StaticResource _rscDeleteImage}" />
                                    </MenuItem.Icon>
                                </MenuItem>
                            </ContextMenu>
                        </ListView.Resources>
                        <ListView.ItemContainerStyle>
                            <Style TargetType="{x:Type ListViewItem}">
                                <Setter Property="telerik:RadContextMenu.ContextMenu" Value="{StaticResource itemContextMenu}" />
                                <EventSetter Event="MouseDoubleClick" Handler="OnDoubleClick_MacroAction" />
                            </Style>
                        </ListView.ItemContainerStyle>
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Model.MacroName}"/>
                                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Model.MacroDescription}" />
                            </GridView>
                        </ListView.View>
                    </ListView>
     
                </Grid>
            </telerik:RadPanelBarItem>

    x
  2. Scott
    Scott avatar
    30 posts
    Member since:
    Aug 2011

    Posted 17 May 2012 Link to this post

    Ok, Solved the problem. Had to use the Telerik context menu with the following configuration. The menu now appears as expected.

            <telerik:RadPanelBarItem Name="_rpbiMacros" Header="Macros" IsDropAllowed="False" Foreground="#FF4D4D4D" BorderBrush="#FFBCB596" >
                <telerik:RadPanelBarItem.Background>
                    <SolidColorBrush Color="{DynamicResource _accordionHeaderColor}"/>
                </telerik:RadPanelBarItem.Background>
                <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu x:Name="_macroActionContextMenu" />
                </telerik:RadContextMenu.ContextMenu>
                <telerik:RadPanelBarItem.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="20">
                            <myIcons:AccordionMacros Height="14" Margin="7,0,11,0"/>
                            <TextBlock TextWrapping="Wrap" Text="Macros" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </telerik:RadPanelBarItem.HeaderTemplate>
                <!--Constrain the size of the wrap panel so it can do its thing-->
                <Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadPanelBar}}, Path=ActualWidth, Converter={StaticResource mathConverter}, ConverterParameter=@VALUE-12;0}">
     
                    <!-- View as icons -->
     
                    <WrapPanel Name="_wrpMacroActions"
                        Visibility="{Binding ViewMacroActionListAsIcons, Converter={StaticResource boolToVisibilityConverter}, ConverterParameter=Visibility.Collapsed}">
                    </WrapPanel>
     
    ... omitted for clarity
     
                    <!-- View as list -->
     
                    <ListView x:Name="_lviewMacroActions" ItemsSource="{Binding MacroActionViewModelList}" ButtonBase.Click="OnClick_MacroActionsListViewHeader"
                        Visibility="{Binding ViewMacroActionListAsIcons, Converter={StaticResource invertBoolToVisibilityConverter}, ConverterParameter=Visibility.Collapsed}" >
                        <ListView.Resources>
                            <telerik:RadContextMenu x:Key="itemContextMenu">
                                <telerik:RadMenuItem Header="Open" Click="OnClick_EditMacroAction">
                                </telerik:RadMenuItem>
                                <telerik:RadMenuItem Header="Delete" Click="OnClick_DeleteMacroAction" >
                                    <telerik:RadMenuItem.Icon>
                                        <ContentControl Template="{StaticResource _rscDeleteImage}" />
                                    </telerik:RadMenuItem.Icon>
                                </telerik:RadMenuItem>
                            </telerik:RadContextMenu>
                        </ListView.Resources>
                        <ListView.ItemContainerStyle>
                            <Style TargetType="{x:Type ListViewItem}">
                                <Setter Property="telerik:RadContextMenu.ContextMenu" Value="{StaticResource itemContextMenu}" />
                                <EventSetter Event="MouseDoubleClick" Handler="OnDoubleClick_MacroAction" />
                            </Style>
                        </ListView.ItemContainerStyle>
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Model.MacroName}"/>
                                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Model.MacroDescription}" />
                            </GridView>
                        </ListView.View>
                    </ListView>
     
                </Grid>
            </telerik:RadPanelBarItem>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Scott
    Scott avatar
    30 posts
    Member since:
    Aug 2011

    Posted 17 May 2012 Link to this post

    Screeching halt! Does not work correctly. The menu shows up but all the menu items are "tied" together. 

    When the RadPanelBarItem does not have the RadContextMenu on it, and the Microsoft ContextMenu is used, the sender in the call to OnClick_EditMacroAction(object sender, RoutedEventArgs e) is the ListViewItem that was right-clicked on to invoke the context menu. That's how it should work.

    When the RadPanelBarItem has the RadContextMenu on it, and the Telerik RadContextMenu is used, the sender in the call to OnClick_EditMacroAction(object sender, RadRoutedEventArgs e) is always the last ListViewItem in the list. It's as if there is only one instance of the RadContextMenu and it's applied to every element in the list. The last one in sets the properties for all.

    This appears to be a bug in Telerik's implementation of their context menus.

    Again, any assistance would be appreciated. Or confirmation that this is expected as a result of the implementation and where SOL using the XAML approach. I will attempt to programmatically attach the context menu thereby insuring that each has it's own copy.
  5. Scott
    Scott avatar
    30 posts
    Member since:
    Aug 2011

    Posted 17 May 2012 Link to this post

    My bad. Had to RTFM - Telerik is doing things differently than MS.

    When using a ListView:

    <ListView x:Name="_lviewMacroActions" ItemsSource="{Binding MacroActionViewModelList}" ButtonBase.Click="OnClick_MacroActionsListViewHeader" SelectionMode="Single"
        Visibility="{Binding ViewMacroActionListAsIcons, Converter={StaticResource invertBoolToVisibilityConverter}, ConverterParameter=Visibility.Collapsed}" >
        <telerik:RadContextMenu.ContextMenu>
            <telerik:RadContextMenu x:Name="_lviewMacroActionsContextMenu">
                <telerik:RadMenuItem Header="Open" Click="OnClick_EditMacroAction" >
                </telerik:RadMenuItem>
                <telerik:RadMenuItem Header="Delete" Click="OnClick_DeleteMacroAction" >
                    <telerik:RadMenuItem.Icon>
                        <ContentControl Template="{StaticResource _rscDeleteImage}" />
                    </telerik:RadMenuItem.Icon>
                </telerik:RadMenuItem>
            </telerik:RadContextMenu>
        </telerik:RadContextMenu.ContextMenu>
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <EventSetter Event="MouseDoubleClick" Handler="OnDoubleClick_MacroAction" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Model.MacroName}"/>
                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Model.MacroDescription}" />
            </GridView>
        </ListView.View>
    </ListView>

    And in the handler, one gets the item as:

    private void OnClick_EditMacroAction(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        System.Windows.Controls.ListViewItem listViewItem = _lviewMacroActionsContextMenu.GetClickedElement<System.Windows.Controls.ListViewItem>();
     
    }

    Sorry about spamming the board. Maybe it'll help the next guy that waits to read the docs.
Back to Top