我有WPF ContextMenu,其MenuItems支持自定义样式。 MenuItem是一个ToggleButton。选中ToggleButton时,会出现带有文本框和关闭按钮的弹出窗口。
<Style TargetType="MenuItem">
<Setter Property="Foreground" Value="{StaticResource ForegroundColor}"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MenuItem" >
<Grid>
<Border Name="Border" Height="25"
Background="{StaticResource BackgroundColor}">
<ToggleButton Name="tbtnOpenPopup">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border Name="externalBorder" Background="Transparent"
BorderThickness="0">
<ContentPresenter Content="{TemplateBinding Content}"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="externalBorder" Property = "Background" Value="{StaticResource DropDownHighlightedBackgroundColor}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="7"/>
</Grid.ColumnDefinitions>
<ContentPresenter ContentSource="Header" VerticalAlignment="Center"
Margin="10,0,0,0"/>
<Path Name="Arrow" Grid.Column="1"
Data="M0,3 L5,0 L0,-3 L0,3 Z"
Fill="{TemplateBinding Foreground}"
VerticalAlignment="Center"/>
</Grid>
</ToggleButton>
</Border>
<Popup Grid.ColumnSpan="2"
Name="Popup"
Placement="Right"
AllowsTransparency="True"
Focusable="False"
StaysOpen="True"
IsOpen="False"
PopupAnimation="None">
<Grid Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding ActualWidth}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="22"/>
</Grid.ColumnDefinitions>
<Border MinHeight="25" Grid.ColumnSpan="2"
x:Name="DropDownBorder"
Background="{StaticResource DropDownBackgroundColor}"
BorderThickness="0"/>
<TextBlock Text="Some text" VerticalAlignment="Top"
HorizontalAlignment="Stretch"/>
<Button Name="closeButton"
Content="X"
Grid.Column="1"
VerticalAlignment="Top"
HorizontalAlignment="Right"/>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="tbtnOpenPopup" Property="IsChecked" Value="true">
<Setter TargetName="Popup" Property="IsOpen" Value="true"/>
<Setter TargetName="Arrow" Property="Data" Value="M5,3 L0,0 L5,-3 L5,3 Z"/>
</Trigger>
<Trigger SourceName="closeButton" Property="IsPressed" Value="true">
<Setter TargetName="tbtnOpenPopup" Property="IsChecked" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想要在按下“closeButton”时关闭弹出文本框,并且ContextMenu保持打开状态。但是当“closeButton”按下ContextMenu关闭时。
我无法理解为什么它很开心。
你有什么想法吗?
答案 0 :(得分:0)
一般来说,在其他地方设置焦点时,上下文菜单会关闭。单击您的ToggleButton是在ContextMenu中,所以没关系 - 但是当您单击关闭按钮时,您在ContextMenu外部单击并关闭。
(请记住,ContextMenu是一个弹出窗口,而不是所有者控件的可视树的一部分。)
答案 1 :(得分:0)
这种类型的要求要求实现基于自定义弹出窗口的上下文菜单,因为上下文菜单将关闭(即使StaysOpen
为真),在上下文菜单之外的其他内容获得焦点后(在您的情况下基于弹出窗口关闭)按钮)。
:(