So I am using the WPFShell to apply chrome to a custom window. I have learned from this article that in order to use it, I have to reference the Microsoft.Windows.Shell library and use this XAML code:

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

My question is, how do I enable chrome by using C# code and not XAML? (i.e. How do I apply chrome in code-behind?)

有帮助吗?

解决方案

Ah, stupid me. It was easy:

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

其他提示

I know that this is a older question, But I noticed that I couldn't get WindowChrome.GetWindowChrome() to work in .NET 4.5. I'm not sure if this has to do with System.Windows.Shell being included within the PresentationFramework assembly. But since it kept returning null there would be no way to update the chrome.

So my solution was to add a 'Name' to the WindowChrome which made it accessible in Code Behind.

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>

Code Behind:

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();
        }
    }
}

I hope this helps someone who needs it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top