要重现,请下载并安装Microsoft Ribbon for WPF。
当用户单击RibbonButton时,我需要显示Popup。此外,当用户单击 Popup 以外的任何位置时,我需要隐藏Popup,因此我必须将属性设置为false。StaysOpen
我创建了一个新的 WPF 功能区应用程序。之后,我更改了默认 MainWindow.xaml 实现,如下所示:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ribbon:Ribbon x:Name="Ribbon">
<ribbon:Ribbon.ApplicationMenu>
<ribbon:RibbonApplicationMenu SmallImageSource="Images\SmallIcon.png">
<ribbon:RibbonApplicationMenuItem Header="Hello _Ribbon"
x:Name="MenuItem1"
ImageSource="Images\LargeIcon.png"/>
</ribbon:RibbonApplicationMenu>
</ribbon:Ribbon.ApplicationMenu>
<ribbon:RibbonTab x:Name="HomeTab"
Header="Home">
<ribbon:RibbonGroup x:Name="Group1"
Header="Group1">
<ribbon:RibbonButton x:Name="Button1"
LargeImageSource="Images\LargeIcon.png"
Label="Button1"
Click="Button1_Click"/>
<Popup PlacementTarget="{Binding ElementName=Button1}"
StaysOpen="False"
Name="p">
<TextBlock Background="Green" Text="Popup Text" />
</Popup>
<Button Click="Button1_Click" Content="Open popup" />
</ribbon:RibbonGroup>
</ribbon:RibbonTab>
</ribbon:Ribbon>
</Grid>
和 MainWindow.xaml.cs:
public partial class MainWindow : RibbonWindow
{
public MainWindow()
{
InitializeComponent();
// Insert code required on object creation below this point.
p.Opened += new EventHandler(p_Opened);
p.Closed += new EventHandler(p_Closed);
}
void p_Closed(object sender, EventArgs e)
{
System.Diagnostics.Debug.Print("closed");
}
void p_Opened(object sender, EventArgs e)
{
System.Diagnostics.Debug.Print("opened");
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
p.IsOpen = true;
}
}
当我启动应用程序时,单击普通的Button,我可以看到Popup。但是当我点击RibbonButton时,我再也看不到它了。我可以在Output
窗口中看到PopupOpened
的事件被引发,但随即Popup的事件被引发。Closed
问:当用户单击 RibbonButton 时,如何显示 StaysOpen=false 的 Popup