我已将上下文菜单添加到WPF中的边框。
<Border>
<Border.ContextMenu>
<ContextMenu x:Name="HistoryPanelContextMenu">
<ContextMenu.Template>
<ControlTemplate>
<Grid Background="{Binding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Background="Transparent" BorderBrush="Transparent" Name="CancelBtn" Content="{x:Static strings:Resource.CancelBtn}" PreviewMouseUp="CancelBtn_OnPreviewMouseUp" Foreground="#fff" FontFamily="Segoe UI Semibold" FontSize="10">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="bdr_main" Background="Transparent" Height="36" VerticalAlignment="Top" BorderBrush="#c0b6d1" BorderThickness="2" CornerRadius="2" Margin="30,0,15,0">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bdr_main" Property="Background" Value="Transparent"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="bdr_main" Property="Background" Value="#7FC0B6D1"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<Button Grid.Column="1" VerticalAlignment="Top" Foreground="#fff" Background="#FF567E94" FontSize="10" Tag="{Binding Id}" PreviewMouseUp="UIElement_OnPreviewMouseUp" Margin="15,5,0,5" FontFamily="Segoe UI Semibold" >
<TextBlock VerticalAlignment="Center">
<Image Height="14" Width="14" Source="/Size.WPF;component/Assets/icon-trash-white.png" Margin="0,0,0,0"/>
<TextBlock Name="DeleteBtnText" Text="{x:Static strings:Resource.DeleteBtnText}"/>
</TextBlock>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="bdr_main" Height="36" Background="#FF567E94" BorderBrush="#FF567E94" BorderThickness="0" CornerRadius="2">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bdr_main" Property="Background" Value="#FF567E94"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="bdr_main" Property="Background" Value="#6596b1"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</ControlTemplate>
</ContextMenu.Template>
</ContextMenu>
</Border.ContextMenu>
</Border>
我尝试在按钮点击时关闭此上下文菜单。我找到解决方案只是为了将IsOpen
设置为false
。好吧,我试着用下一个方式:
HistoryPanelContextMenu.IsOpen = false;
但是HistoryPanelContextMenu
未定义,我不知道为什么。
那么我如何在点击时关闭上下文菜单?
感谢。
答案 0 :(得分:1)
您是否尝试在上下文菜单中绑定鼠标事件?
<ContextMenu x:Name="TimeCardGridContextMenu" MouseLeftButtonUp="DoSomething">
并使用发件人或事件args访问您需要的东西?