代码之家  ›  专栏  ›  技术社区  ›  ajb Vincent McNabb

WPF上下文菜单“单击事件”在一个位置起作用,而不是在另一个位置起作用

  •  1
  • ajb Vincent McNabb  · 技术社区  · 7 年前

    我有以下XAML,它生成一个列表框,其中每个项在扩展器中包含另一个列表框,并且我已经定义了 PageContextMenu 作为顶级列表的上下文菜单 FrameContextMenu 对于较低级别的列表。

    问题:两者都显示正确,但单击事件仅在顶层上下文菜单上工作,而不是在底层上下文菜单上工作。例如,单击 Delete Selected 在里面 页面上下文菜单 正确调用关联的处理程序,但单击 Delete Selected Frame(s) 在里面 框架上下文菜单 不激发关联的处理程序。我没有看到任何错误的迹象,即使我在 ContextDeleteFrames_Click 它不会被击中。就好像根本没有与该菜单项相关联的处理程序。

    我看过其他一些与上下文菜单不起作用有关的问题,但似乎都不适用。嵌套的两个列表框有问题吗?

    XAML:

    <ListBox Name="PageListBox" ItemsSource="{Binding CurrentPack.Pages}" HorizontalAlignment="Stretch" SelectionMode="Extended">
    <ListBox.Resources>
        <ContextMenu x:Key="PageContextMenu">
            <MenuItem Header="_Add" Name="ContextAddAddPage"/>
            <MenuItem Header="_Edit" Name="ContextEditPage"/>
            <MenuItem Header="_Delete Selected" Name="ContextDeletePage" Click="ContextDeletePage_Click"/>
        </ContextMenu>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="ContextMenu" Value="{StaticResource PageContextMenu}"/>
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
        </Style>
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate >
            <Border  BorderThickness="2" BorderBrush="White" HorizontalAlignment="Stretch">
                <StackPanel HorizontalAlignment="Stretch">
                    <Label Content="{Binding PresentationName}"/>
                    <Expander  VerticalAlignment="Top" HorizontalAlignment="Stretch">
                        <Expander.Header>
                            <Label Content="{Binding FrameStatusText}"/>
                        </Expander.Header>
                        <ListBox Name="FrameListBox" ItemsSource="{Binding Frames}" HorizontalAlignment="Stretch" SelectionMode="Extended">
                            <ListBox.Resources>
                                <ContextMenu x:Key="FrameContextMenu">
                                    <MenuItem Header="_Add Frame" Name="ContextAddFrame"/>
                                    <MenuItem Header="_Edit Frame" Name="ContextEditFrame"/>
                                    <MenuItem Header="_Delete Selected Frame(s)" Name="ContextDeleteFrames" Click="ContextDeleteFrames_Click"/>
                                    <MenuItem Header="Show _Preview" Name="ContextShowPreview" Click="ContextShowPreview_Click"/>
                                </ContextMenu>
                                <Style TargetType="{x:Type ListBoxItem}">
                                    <Setter Property="ContextMenu" Value="{StaticResource FrameContextMenu}"/>
                                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                                </Style>
                            </ListBox.Resources>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding PresentationName}"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Expander>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
    

    代码隐藏:

        private void ContextDeletePage_Click(object sender, RoutedEventArgs e)
        { //this works
            Workspace.Content.DeleteSelectedPages();
        }
    
        private void ContextDeleteFrames_Click(object sender, RoutedEventArgs e)
        { //this doesn't!
            Workspace.Content.DeleteSelectedFrames();
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   dymanoid    7 年前

    DataTemplate

    PageListBox

    <ListBox Name="PageListBox">
        <ListBox.Resources>
            <!-- ... other resources... -->
    
            <ContextMenu x:Key="FrameContextMenu">
                <MenuItem Header="_Add Frame" Name="ContextAddFrame"/>
                <MenuItem Header="_Edit Frame" Name="ContextEditFrame"/>
                <MenuItem Header="_Delete Selected Frame(s)" Click="ContextDeleteFrames_Click"/>
                <MenuItem Header="Show _Preview" Name="ContextShowPreview" Click="ContextShowPreview_Click"/>
            </ContextMenu>
        </ListBox.Resources>
    </ListBox>
    

    <MenuItem Header="_Delete Selected Frame(s)" Command="{Binding DeleteFrameCommand}"/>
    

    DeleteFrameCommand ICommand RoutedCommand

    PlacementTarget PlacementTarget.Tag

    推荐文章