如何从 WPF UserControl 中删除 Windows 边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16269849/
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 08:42:11  来源:igfitidea点击:

How to remove Windows border from WPF UserControl?

c#wpfxamlblend

提问by Tevis

To preface this question, I am working on coding the back end of an application whose UI was put together by someone else (I believe using Blend). The application consists of a series of "Screens," whose root element in XAML is "UserControl". There is no use of the "Window" tag anywhere in the source.

作为这个问题的序言,我正在编写一个应用程序的后端,该应用程序的 UI 是由其他人组装的(我相信使用 Blend)。该应用程序由一系列“屏幕”组成,其 XAML 中的根元素是“UserControl”。源代码中的任何地方都没有使用“Window”标签。

What I want to do is remove the Windows border that is added to the outside edge of the application when I run the program. The border currently consists of forward/backward buttons like a web browser, and an X button to close.

我想要做的是删除运行程序时添加到应用程序外边缘的 Windows 边框。边框当前由向前/向后按钮(如 Web 浏览器)和用于关闭的 X 按钮组成。

All I can find from searches are instructions to add

我能从搜索中找到的只是添加说明

WindowStyle="None"

to the

<Window>

element. But of course, I don't have one of those, and WindowStyle is not a property of UserControl. Anyone know how to accomplish this with UserControl root elements?

元素。但当然,我没有其中之一,而且 WindowStyle 不是 UserControl 的属性。任何人都知道如何使用 UserControl 根元素完成此操作?

Edit:The StartupUri for the application is

编辑:应用程序的 StartupUri 是

this.StartupUri = new Uri(@"pack://application:,,,/WpfPrototype1.Screens;Component/Screen_1.xaml");

the file it points to does not have a Window tag.

它指向的文件没有 Window 标签。

回答by sa_ddam213

Based on the comments above it seems your MainWindowis created dynamically somewhere, however you can use the Applicationclass to get the applications MainWindow.

根据上面的评论,您似乎MainWindow是在某处动态创建的,但是您可以使用Application该类来获取应用程序MainWindow

var mainWindow = Application.Current.MainWindow;

And you can then set your border style from there

然后你可以从那里设置你的边框样式

Example:

例子:

private void RemoveBorder()
{
    var mainWindow = Application.Current.MainWindow;
    if (mainWindow != null)//should never be
    {
        mainWindow.WindowStyle = System.Windows.WindowStyle.None; // removes top bar (icon, title, close buttons etc)
        mainWindow.AllowsTransparency = true; //removes the border around the outside
    }
}