1

要重现,请下载并安装Microsoft Ribbon for WPF

当用户单击RibbonButton时,我需要显示Popup。此外,当用户单击 Popup 以外的任何位置时我需要隐藏Popup,因此我必须将属性设置为falseStaysOpen

我创建了一个新的 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

4

1 回答 1

4

检查此处发布的问题(我自己也尝试过),解决方案的一半是使用 上的ClickMode="Press"属性RibbonButton来打开弹出窗口。这会产生另一个问题,因为它似乎阻止了所有未来RibbonButton.Click事件的触发,而且我还没有设法找到阻止它的原因(我仍然是 WPF 菜鸟;))

另一种解决方案是移动控件Popup外部Ribbon,然后它开始按预期运行。

<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"/>

                <Button Click="Button1_Click" Content="Open popup" />

            </ribbon:RibbonGroup>
        </ribbon:RibbonTab>
    </ribbon:Ribbon>
    <Popup PlacementTarget="{Binding ElementName=Button1}"
                   StaysOpen="False"
                   Name="p">
        <TextBlock Background="Green" Text="Popup Text" />
    </Popup>
</Grid>
于 2013-03-07T06:46:20.383 回答