带有弹出文本框的菜单项
我有 WPF ContextMenu 和支持自定义样式的 MenuItems。 MenuItem 是一个切换按钮。当选中切换按钮时,会出现带有文本框和关闭按钮的弹出窗口。
<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”时关闭弹出文本框,并且上下文菜单保持打开状态。但是当按下“closeButton”时,ContextMenu 关闭。
我不明白为什么会这样。
你有什么想法吗?
I have WPF ContextMenu with MenuItems that support custom style.
MenuItem is a ToggleButton. When the ToggleButton is checked the popup with textbox and close button appears.
<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>
I want the popup textbox to close when the "closeButton" pressed, and the ContextMenu to stays open. But when the "closeButton" pressed the ContextMenu closed.
I couldn't understand why it's happining.
Have you any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,当焦点设置在其他地方时,上下文菜单将关闭。单击您的 ToggleButton 是在 ContextMenu 内,所以没关系 - 但是当您单击关闭按钮时,您正在单击 ContextMenu 外部,并且它会关闭。
(请记住,ContextMenu 是弹出窗口,而不是所有者控件可视化树的一部分。)
Generally speaking, a context menu will close when focus is set elsewhere. Clicking your ToggleButton is within the ContextMenu, so that's okay - but when you click on the close button, you're clicking outside the ContextMenu and it closes.
(Remember that a ContextMenu is a popup, and not part of the visual tree of the owner control.)
这种类型的要求要求实现基于自定义弹出窗口的上下文菜单,因为上下文菜单将关闭(即使
StaysOpen
为 true),在上下文菜单之外的其他内容获得焦点之后(在您的情况下,基于弹出窗口的关闭按钮)。:(
This type of requirement calls for implementing a custom popup based context menu as context menus will close (even is
StaysOpen
is true), after something else outside the context menu gets focus (in your case the popup based close button).:(