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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39243463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:55:09  来源:igfitidea点击:

How to hide 'Minimize', 'Maximize' and 'Close' buttons from PrismUserControl WPF?

c#wpfmvvmprism

提问by Prohor

In my Prism 6 WPF MVVM application I use the following PrismUserControl WPF for displaying of modal notification dialogs:

在我的 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>

In the Views where I use this UserControl as a modal dialog content I define it as following:

在我将此 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>

When I activate the dialog it is displayed, for example, as the following: enter image description here

当我激活对话框时,它会显示,例如,如下所示: 在此处输入图片说明

But as you can see 'Minimize', 'Maximize' and 'Close' buttons are visible and enabled. And the system menu (activated in upper left corner of the dialog) is enabled too. How can I hide 'Minimize', 'Maximize' and 'Close' buttons and disable the system menu?

但是正如您所看到的,“最小化”、“最大化”和“关闭”按钮是可见的并已启用。并且系统菜单(在对话框的左上角激活)也被启用。如何隐藏“最小化”、“最大化”和“关闭”按钮并禁用系统菜单?

回答by Brian Lagunas

@Evangelink was close. You would use the WindowStyle property, but you must supply an actual Style. Something like:

@Evangelink 很接近。您将使用 WindowStyle 属性,但您必须提供实际的 Style。就像是:

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

回答by Yiao SUN

2.Disabling close buttons:

2.禁用关闭按钮:

Not showing Icon and Close button: Unfortunately, this feature is not available in WPF. To achieve this, you can try setting the WS_EX_DLGMODALFRAME window style by calling [Get/Set]WindowLong (pInvoking in to Win32) from the SourceInitialized event on the Window class. Check this:

不显示图标和关闭按钮:不幸的是,此功能在 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;
  }
}

回答by Manish Dubey

I have a workaround, if it suits your structure. Expose a Usercontrol loaded event in code behind (can be achieved also using MVVM) of your Usercontrol NotificationDialogPopupView

我有一个解决方法,如果它适合您的结构。在用户控件 NotificationDialogPopupView 的后面代码(也可以使用 MVVM 实现)中公开用户控件加载事件

Loaded="UserControl_Loaded"

and write down following code in the Loaded event

并在 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;

            }
        }

回答by Amaury Levé

I don't have any working example right now so I might have an uncomplete answer.

我现在没有任何有效的例子,所以我可能有一个不完整的答案。

By looking to Prism source code PopupWindowActionI can see a WindowStyleproperty that you can use to change the style of the popup. I guess the property value you are looking for is WindowStyle="ToolWindow".

通过查看 Prism 源代码PopupWindowAction,我可以看到一个WindowStyle可用于更改弹出窗口样式的属性。我猜你正在寻找的财产价值是WindowStyle="ToolWindow".

Hope it helps!

希望能帮助到你!

回答by Yiao SUN

I found two solutioins :

我找到了两个解决方案:

1.Disable all windows property :

1.禁用所有窗口属性:

Set the Window.WindowStyleproperty to WindowStyle.None

Window.WindowStyle属性设置为WindowStyle.None

2.Disabling buttons:

2.禁用按钮:

I. Disabling Minimize, Maximize buttons :
This can be achieved by setting Window.ResizeModeproperty to ResizeMode.NoResize. It will disable the minimize and maximize buttons. Furthermore, the window will not resize by mouse click+drag.

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

II. Not showing Icon and Close button:
Unfortunately, this feature is not available in WPF. To achieve this, you can try setting the WS_EX_DLGMODALFRAME window style by calling [Get/Set]WindowLong (pInvoking in to Win32) from the SourceInitialized event on the Window class.

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