上下文菜单左键单击
Context Menu Left Click
我有一个上下文菜单,它曾经是一个弹出菜单。当我第一次加载程序时,上下文菜单是空的,因为菜单项绑定到列表。但是,如果我加载程序并首先右键单击列表就可以了,然后我可以左键单击并显示列表。我将显示图片来帮助我的描述:
先左键单击
先右击再左击
任何人都可以帮助我了解为什么会发生这种情况以及如何解决它吗?
编辑 - 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 结果,现在唯一的问题是我想更改突出显示的背景鼠标悬停在 "Add Existing Property" 上 有谁知道如何做到这一点并将菜单移到右边 我会 post 一张图片来解释
<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
来实现,如下所示,我认为没有必要在 MenuItem
中添加 ListBox
<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>
我有一个上下文菜单,它曾经是一个弹出菜单。当我第一次加载程序时,上下文菜单是空的,因为菜单项绑定到列表。但是,如果我加载程序并首先右键单击列表就可以了,然后我可以左键单击并显示列表。我将显示图片来帮助我的描述:
先左键单击
先右击再左击
任何人都可以帮助我了解为什么会发生这种情况以及如何解决它吗?
编辑 - 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 结果,现在唯一的问题是我想更改突出显示的背景鼠标悬停在 "Add Existing Property" 上 有谁知道如何做到这一点并将菜单移到右边 我会 post 一张图片来解释
<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
来实现,如下所示,我认为没有必要在 MenuItem
ListBox
<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>