右键单击上下文菜单

瑞安·阿奇博尔德(Ryan Archibald)

我有一个上下文菜单,它曾经是一个弹出菜单。当我第一次加载程序时,上下文菜单为空,因为菜单项绑定到了列表。但是,如果我加载程序并先单击鼠标右键,则列表会很好,然后可以单击鼠标左键,列表就会出现。我将显示图像以帮助我进行描述:

先左键单击

在此处输入图片说明

先右键单击,然后再单击鼠标左键

在此处输入图片说明

谁能帮助我了解为什么会这样以及如何解决?

编辑-Xaml

 <DataTemplate x:Key="AddNodeTemplate">
        <StackPanel>
            <Button x:Name="buttonAdd">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Style.Triggers>
                            <EventTrigger RoutedEvent="Click">
                                <EventTrigger.Actions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                                <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
                                            </BooleanAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger.Actions>
                            </EventTrigger>
                        </Style.Triggers>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="Button">
                                    <Border BorderThickness="0">
                                        <ContentPresenter/>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Button.Style>
                <Canvas>
                    <Image Source="Images/bkg_plus.png" Width="30" Height="30" Panel.ZIndex="5"/>
                    <Rectangle Stroke="LightGray" StrokeDashArray="2 2" Width="120" Height="30" VerticalAlignment="Center" Margin="0,0,0,0"
                Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type syncfusion:Node}}}">
                        <Rectangle.Fill>
                            <SolidColorBrush Color="LightGray"/>
                        </Rectangle.Fill>
                    </Rectangle>
                    <TextBlock Text="Add Property" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="30 10 0 0" Background="Transparent"/>
                </Canvas>
                <Button.ToolTip>
                    <ToolTip>
                        <Label Content="Add Property"/>
                    </ToolTip>
                </Button.ToolTip>
                <Button.ContextMenu>
                    <ContextMenu ItemsSource="{Binding AvailableProperties}">
                        <ContextMenu.Resources>
                            <Style TargetType="MenuItem">
                                <Setter Property="Header" Value="{Binding Path=Name}"/>
                                <Setter Property="ToolTip" Value="{Binding Path=ToolTips}"/>
                                <Setter Property="Command" Value="{Binding Path=Command}"/>
                                <Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
                                <!--<Setter Property="Icon" Value="{Binding Icon , Converter={StaticResource ImageToSourceConverter}"/>-->
                            </Style>
                        </ContextMenu.Resources>
                    </ContextMenu>
                </Button.ContextMenu>
            </Button>
        </StackPanel>
    </DataTemplate>

新结果

好的,这就是当我先单击鼠标左键时发生的情况

在此处输入图片说明

因此,我尝试使用弹出窗口并在其中包含一个菜单,这是我的XAML结果,现在唯一的问题是当鼠标悬停在“添加现有属性”上时,我想更改突出显示的背景。如何做到这一点,以及将菜单移至右侧,我将张贴图片以进行解释

 <DataTemplate x:Key="AddNodeTemplate">
        <Border BorderThickness="1" Background="#F7F7F7">
            <Border.BorderBrush>
                <DrawingBrush Viewport="8,8,8,8" ViewportUnits="Absolute" TileMode="Tile">
                    <DrawingBrush.Drawing>
                        <DrawingGroup>
                            <GeometryDrawing Brush="#F7F7F7">
                                <GeometryDrawing.Geometry>
                                    <GeometryGroup>
                                        <RectangleGeometry Rect="0,0,50,50"/>
                                        <RectangleGeometry Rect="50,50,50,50"/>
                                    </GeometryGroup>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>
                        </DrawingGroup>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Border.BorderBrush>
            <StackPanel>
            <ToggleButton Height="30" Width="120" Style="{StaticResource ChromelessToggleButton}" x:Name="toggleButtonAdd" IsHitTestVisible="{Binding ElementName=Popup, Path=IsOpen, Mode=OneWay, Converter={StaticResource OppositeBooleanConverter}}">
                <ToggleButton.ToolTip>
                    <ToolTip>
                        <Label Content="Add Property"/>
                    </ToolTip>
                </ToggleButton.ToolTip>
            </ToggleButton>
            <Popup IsOpen="{Binding IsChecked, ElementName=toggleButtonAdd}" x:Name="Popup" StaysOpen="False" Placement="Right">
                <Border BorderBrush="Black" BorderThickness="0" Background="#F7F7F7">
                        <StackPanel Margin="5,10,5,5" Orientation="Horizontal">
                            <Menu Background="#F7F7F7">
                                <Menu.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <VirtualizingStackPanel Orientation="Vertical"/>
                                    </ItemsPanelTemplate>
                                </Menu.ItemsPanel>
                            <MenuItem Header="Add Exisiting Properties">
                                <ListBox BorderBrush="Black" BorderThickness="0" Background="Transparent" Margin="5" Padding="4" Width="130"
                                 ItemsSource="{Binding Path=AvailableProperties}" SelectionChanged="Selector_OnSelectionChanged">
                                    <ListBox.ItemContainerStyle>
                                        <Style TargetType="ListBoxItem">
                                            <Style.Triggers>
                                                <Trigger Property="IsSelected" Value="True">
                                                    <Setter Property="FontWeight" Value="Bold"/>
                                                    <Setter Property="Background" Value="Transparent"/>
                                                    <Setter Property="Foreground" Value="Black"/>
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Background" Value="#5194C7"/>
                                                    <Setter Property="FontWeight" Value="Bold"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ListBox.ItemContainerStyle>
                                    <ListBox.ItemTemplate>
                                    <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <Image Source="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}" Width="12" Height="12" Margin="3" VerticalAlignment="Center"/>
                                                <TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
                                            </StackPanel>
                                    </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </MenuItem>
                                <MenuItem Header="Upscale Well logs">        
                                </MenuItem>
                                <MenuItem Header="Upscale well_top attributes">
                                </MenuItem>
                                <MenuItem Header="Upscale point attributes">
                                </MenuItem>
                                <MenuItem Header="Calculate">
                                </MenuItem>
                            </Menu>
                    </StackPanel>
                </Border>
            </Popup>
        </StackPanel>
        </Border>
    </DataTemplate>

菜单图片位于弹出窗口的右下方,而不是如上图所示的右侧,我希望背景为蓝色而不是灰色背景,如下图所示

在此处输入图片说明

您可以通过在下面添加Context Menu来实现此目的Style,我认为不必将其与ListBox内部一起使用MenuItem

 <Button.ContextMenu>
  <ContextMenu ItemsSource="{Binding AvailableProperties}">
    <ContextMenu.Resources>
       <Style TargetType="MenuItem">                            
          <Setter Property="Header" Value="{Binding Path=Name}"/>
          <Setter Property="ToolTip" Value="{Binding Path=ToolTips}"/>
          <Setter Property="Command" Value="{Binding Path=Command}"/>
          <Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
          <Setter Property="Icon" Value="{Binding Icon , Converter={StaticResource ImageToSourceConverter}"/>
       </Style>
     </ContextMenu.Resources>                    
   </ContextMenu>
 </Button.ContextMenu>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Java Swing中创建右键单击上下文菜单?

如何在JavaScript中禁用右键单击上下文菜单

禁止右键单击元素打开上下文菜单

闪亮:在numericInput上单击鼠标右键提供上下文菜单吗?

找到某人右键单击时光标所处的tr并选择上下文菜单

如何检测超链接的右键单击上下文菜单中的哪个选项被单击?

用PyGObject右键单击上下文菜单

如何创建上下文菜单,这些菜单在oracle apex报表中单击鼠标右键?

主顶部菜单和右键单击上下文菜单之间的WPF共享菜单

右键单击Javascript切换默认和自定义上下文菜单

将安全删除硬件选项添加到桌面右键单击上下文菜单中?

如何从Windows的右键单击(上下文)菜单中删除项目?

从右键单击(上下文)菜单中删除Windows内置项目

禁用右键单击上下文菜单?

在Windows右键单击上下文菜单中添加“在此处打开MSYS终端”?

如何在TableView rowDelegate中单击鼠标右键显示上下文菜单

右键单击添加上下文菜单。

右键单击上下文菜单长按Qt

AutoHotkey脚本,用于“右键单击上下文菜单”命令

AutoHotkey脚本,用于“右键单击上下文菜单”命令

如何在窗口10的右键单击上下文菜单中添加cmd删除选项

右键单击Android上下文菜单

右键单击上下文菜单后,获取选定的项目

Python IDLE从右键单击上下文菜单中消失了

如何从Windows 10的右键单击上下文菜单中启动MobaXterm

在右键菜单中将右键单击目标传递给上下文相关的应用程序

仅在桌面添加右键单击上下文菜单

右键单击系统托盘中的通知图标不显示上下文菜单

在 React 中禁用 Firefox 的右键单击上下文菜单