[英]Close Context Menu in WPF
I've add context menu to border in WPF. 我在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>
I tried to close this context menu on button click. 我试图在单击按钮时关闭此上下文菜单。 I found solution just to set
IsOpen
to false
. 我找到了将
IsOpen
设置为false
解决方案。 Well, I tried it in next way : 好吧,我尝试了以下方式:
HistoryPanelContextMenu.IsOpen = false;
but HistoryPanelContextMenu
is undefined and I don't know why. 但是
HistoryPanelContextMenu
是未定义的,我不知道为什么。
So How I can close context menu on click? 那么,如何单击才能关闭上下文菜单?
Thanks. 谢谢。
Did You try binding to a mouse event on the context menu ? 您是否尝试在上下文菜单上绑定到鼠标事件?
<ContextMenu x:Name="TimeCardGridContextMenu" MouseLeftButtonUp="DoSomething">
and the use the sender or the event args to access stuff You need ? 以及使用发送方或事件args访问所需的内容?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.