如何使用C#代码而不是XAML来启用WindowChrome?

如何使用C#代码而不是XAML来启用WindowChrome?,c#,wpf,xaml,window-chrome,C#,Wpf,Xaml,Window Chrome,因此,我正在使用将chrome应用于自定义窗口。我从中了解到,为了使用它,我必须参考并使用以下XAML代码: <shell:WindowChrome.WindowChrome> <shell:WindowChrome ResizeBorderThickness="6" CaptionHeight="43" CornerRadius="25,25,10,10" GlassFrameThickness="0"> </she

因此,我正在使用将chrome应用于自定义窗口。我从中了解到,为了使用它,我必须参考并使用以下XAML代码:

<shell:WindowChrome.WindowChrome>
    <shell:WindowChrome
    ResizeBorderThickness="6"
    CaptionHeight="43"
    CornerRadius="25,25,10,10"
    GlassFrameThickness="0">
    </shell:WindowChrome>
</shell:WindowChrome.WindowChrome>


我的问题是,如何使用C代码而不是XAML启用chrome?(即如何在代码隐藏中应用chrome?

啊,愚蠢的我。这很容易:

WindowChrome.SetWindowChrome(this, new WindowChrome());

我知道这是一个老问题,但我注意到我无法在.NET4.5中使用
WindowChrome.GetWindowChrome()
。我不确定这是否与
System.Windows.Shell
包含在
PresentationFramework
程序集中有关。但由于它不断返回
null
,因此无法更新chrome

因此,我的解决方案是在
WindowChrome
中添加一个“名称”,这样就可以在代码隐藏中访问它

XAML:

我希望这能帮助有需要的人

<Window x:Class="SomeProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    mc:Ignorable="d"Title="Some Window" WindowStyle="None" ResizeMode="CanResize" 
    AllowsTransparency="True">

    <WindowChrome.WindowChrome>
        <WindowChrome x:Name="chrome" ResizeBorderThickness="6" CaptionHeight="0"
                      GlassFrameThickness="0" CornerRadius="0" UseAeroCaptionButtons="False"/>
    </WindowChrome.WindowChrome>

</window>
using System;
using System.Window;    

namespace SomeProject
{
    public partial class MainWindow: Window
    {
        public MainWindow()
        {
            //Get Existing 'WindowChrome' Properties.
            var captionHeight = chrome.CaptionHeight;

            //Set Existing 'WindowChrome' Properties.
            chrome.GlassFrameThickness = new Thickness(2d);

            //Assign a New 'WindowChrome'.
            chrome = new System.Windows.Shell.WindowChrome();
        }
    }
}