上下文菜单左键单击

时间:2015-10-15 14:45:50

标签: wpf xaml contextmenu

我有一个上下文菜单,曾经是一个弹出菜单。当我第一次加载程序时,上下文菜单为空,因为菜单项绑定到列表。但是,如果我加载程序并右键单击列表就可以了,然后我可以转到左键单击并显示列表。我将展示图片以帮助我的描述:

先左键点击

enter image description here

先点击鼠标右键,然后在

后点击鼠标左键

enter image description here

任何人都可以帮助我理解为什么会发生这种情况以及如何解决它?

编辑 - 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>

新结果

好的,现在这就是当我先点击鼠标时发生的事情

enter image description here

所以我尝试使用弹出窗口并在其中有一个菜单,这是我的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>

弹出窗口下方的菜单图片,而不是像上面的图片一样,我希望背景为蓝色而不是灰色背景,如下图所示

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过添加Context MenuStyle来完成此操作,我认为不必在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>