如何从PrismUserControl WPF隐藏“最小化”、“最大化”和“关闭”按钮?

3

在我的Prism 6 WPF MVVM应用程序中,我使用以下的PrismUserControl WPF来显示模态通知对话框:

<UserControl x:Class="CommonWpfControlLibrary.NotificationDialogPopupView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
         xmlns:prism="http://prismlibrary.com/"             
         prism:ViewModelLocator.AutoWireViewModel="True"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" MaxHeight="300" MaxWidth="600">

    <StackPanel Orientation="Vertical" Margin="20">
        <TextBlock Text="{Binding Message}" TextWrapping="Wrap"/>
        <telerik:RadButton Content="OK" Command="{Binding OnOkPressedCommand}" HorizontalAlignment="Center" Width="50" Margin="0 10 0 0"/>
    </StackPanel>
</UserControl>

在使用此UserControl作为模态对话框内容的视图中,我将其定义如下:
<i:Interaction.Triggers>
    <prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest, Mode=OneWay}">
        <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
            <prism:PopupWindowAction.WindowContent>
                <commonControls:NotificationDialogPopupView/>
            </prism:PopupWindowAction.WindowContent>
        </prism:PopupWindowAction>
    </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>

当我激活对话框时,它会显示为以下内容,例如: enter image description here 但是,可以看到“最小化”、“最大化”和“关闭”按钮是可见且启用的。并且系统菜单(在对话框左上角激活)也是启用的。如何隐藏“最小化”、“最大化”和“关闭”按钮并禁用系统菜单?
5个回答

2
我找到了两种解决方案:

1. 禁用所有窗口属性:

Window.WindowStyle属性设置为WindowStyle.None

2. 禁用按钮:

I. 禁用最小化和最大化按钮:
可以通过设置Window.ResizeMode属性为ResizeMode.NoResize来实现。这将禁用最小化和最大化按钮。此外,窗口将不会通过鼠标单击+拖动来调整大小。

II. 不显示图标和关闭按钮:
不幸的是,这个功能在WPF中不可用。为了实现这个目标,您可以尝试通过在Window类上的SourceInitialized事件中调用[Get/Set]WindowLong(pInvoking in to Win32)来设置WS_EX_DLGMODALFRAME窗口样式。


UserControl WPF没有WindowStyle和ResizeMode属性。 - Prohor
1
在窗口属性中,ResizeMode="NoResize" 表示禁止调整大小。或者在 .cs 文件中,this.ResizeMode = System.Windows.ResizeMode.NoResize; 也表示禁止调整大小。用户控件可以加载在窗口中吗?@Prohor - Yiao SUN
userControl只是一个视图,你可以在窗口中加载它。请查看这个链接:http://stackoverflow.com/a/36042795/6773405 @Prohor - Yiao SUN
1
我是这样做的: prism:PopupWindowAction.WindowStyle <Style TargetType="Window"> <Setter Property="ResizeMode" Value="NoResize"/> <Setter Property="SizeToContent" Value="WidthAndHeight"/> </Style> </prism:PopupWindowAction.WindowStyle> 它有效。 - Prohor
姚孙,您能否详细说明如何在模态对话框中隐藏“关闭”按钮、图标和系统菜单? - Prohor

2

@Evangelink接近正确。您需要使用WindowStyle属性,但必须提供实际样式。类似以下内容:

<Style TargetType="{Window}">
     <Setter Property="" Value="" />
</Style>

1

2. 禁用关闭按钮:

不显示图标和关闭按钮: 很遗憾,这个功能在 WPF 中不可用。要实现此功能,您可以尝试通过在 Window 类的 SourceInitialized 事件中调用 [Get/Set]WindowLong (pInvoking in to Win32) 来设置 WS_EX_DLGMODALFRAME 窗口样式。 请参考:

public partial class MainWindow : Window
{

[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);



const uint MF_BYCOMMAND = 0x00000000;
const uint MF_GRAYED = 0x00000001;
const uint MF_ENABLED = 0x00000000;

const uint SC_CLOSE = 0xF060;

const int WM_SHOWWINDOW = 0x00000018;
const int WM_CLOSE = 0x10;

public MainWindow()
{
    InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{

}

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);

    HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;

    if (hwndSource != null)
    {
        hwndSource.AddHook(new HwndSourceHook(this.hwndSourceHook));
    }
}


IntPtr hwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == WM_SHOWWINDOW)
    {
        IntPtr hMenu = GetSystemMenu(hwnd, false);
        if (hMenu != IntPtr.Zero)
        {
            EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
        }
    }
    else if (msg == WM_CLOSE)
    {
        handled = true;
    }
    return IntPtr.Zero;
  }
}

以下是如何隐藏关闭按钮的方法,当您想要关闭时,可以使用 Alt + F4 快捷键。-@Prohor - Yiao SUN
谢谢。但是我的应用程序是Prism多模块应用程序。这意味着它由主窗口和许多视图组成,每个视图都是位于单独模块中的UserControl。因此,我不仅在主窗口中使用通知对话框,还在其他作为UserControl的视图中使用。您的方法是否适用于UserControl而不仅仅是Window? - Prohor
这只适用于窗口,我建议你创建另一个窗口专门用于此目的,就像你创建了一个名为window2的新窗口一样。window2 win2 = new window2(); win2.Show(); this.Close();你可以在window1中使用事件或其他方式编写这些代码,就像你在usercontrol中所做的那样,在这个窗口中你只需添加你需要的内容。 - Yiao SUN
我在WPF的GitHub存储库中开了一个问题,以便对这些按钮进行投票,并增加更多的控制权:https://github.com/dotnet/wpf/issues/4294 - Thraka

1
我有一个解决办法,如果适合你的结构。在代码后台公开一个UserControl加载事件(也可以使用MVVM)你的Usercontrol NotificationDialogPopupView。
Loaded="UserControl_Loaded"

在Loaded事件中写下以下代码。
private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Window win = ((UserControl)sender).Parent as Window;
            if(win != null)
            {
                win.WindowStyle = WindowStyle.None;
                win.ResizeMode = ResizeMode.NoResize;

            }
        }

0

我现在没有任何可用的示例,所以我的答案可能不完整。

通过查看 Prism 源代码 PopupWindowAction,我可以看到一个 WindowStyle 属性,你可以使用它来改变弹出窗口的样式。我猜你要找的属性值是 WindowStyle="ToolWindow"

希望能对你有所帮助!


它产生了以下错误:"样式的类型转换器类不支持从字符串进行转换"。 - Prohor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接