如何在WPF中将样式应用于Window Control?

前端之家收集整理的这篇文章主要介绍了如何在WPF中将样式应用于Window Control?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为App.xaml中的Window设置样式,如下所示:
<Application x:Class="MusicRepo_Importer.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" StartupUri="TestMaster.xaml">
    <Application.Resources>

        <Style TargetType="Window">
            <Setter Property="WindowStyle" Value="None"></Setter>
        </Style>

    </Application.Resources>
</Application>

我基本上希望每个Window都将其WindowStyle的属性值设置为None(删除默认的Windows框架和边框);但它没有用.

我在这里想念的是什么?

我相信你必须将样式命名并将其应用于每个窗口,如下所示.

在app.xaml / resources ..

<Style x:Key="MyWindowStyle" TargetType="Window">
    <Setter Property="WindowStyle" Value="None"></Setter>
</Style>

然后在window.xaml ..

<Window x:Class="MusicRepo_Importer.MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="MyStyledWindow" Style="{StaticResource MyWindowStyle}">

这应该可行,但只是在资源中应用具有TargetType for Window的样式将不会强制Window使用该样式,尽管它似乎适用于其他元素.

编辑:
找到一些与将默认样式应用于窗口元素有关的信息.

If you supply a TargetType,all
instances of that type will have the
style applied. However derived types
will not… it seems. <Style
TargetType=”{x:Type Window}”> will not
work for all your custom
derivations/windows. <Style
TargetType=”{x:Type local:MyWindow}”>
will apply to only MyWindow. So the
options are

Use a Keyed Style that you specify as
the Style property of every window you
want to apply the style. The designer
will show the styled window.

来自问题:How to set default WPF Window Style in app.xaml?

回答问题的人对从具有应用样式的基本窗口继承有一个有趣的想法.

原文链接:https://www.f2er.com/windows/364844.html

猜你在找的Windows相关文章