Implementation Failure of a Transparent Window in WPF
<p>I found the implementation of WPF transparent window WITH BORDERS here <a href="https://stackoverflow.com/questions/16927699/c-sharp-wpf-transparent-window-with-a-border">C# WPF transparent window with a border</a>. I tried implementing it, but my window is not transparent. No errors come out. Below are the xaml and code behind.</p>

<pre><code>&lt;Window x:Class="WpfApplication9.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Extended Glass in WPF" Height="300" Width="400" 
    Background="Transparent" Loaded="Window_Loaded"&gt;

    &lt;Grid ShowGridLines="True" Background="Transparent"&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>

<p>Code behind</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Drawing;

namespace WpfApplication9
{
    /// &lt;summary&gt;
    /// Interaction logic for MainWindow.xaml
    /// &lt;/summary&gt;
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var transparancyConverter = new TransparancyConverter(this);
            transparancyConverter.MakeTransparent();
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int cxLeftWidth;      // width of left border that retains its size
            public int cxRightWidth;     // width of right border that retains its size
            public int cyTopHeight;      // height of top border that retains its size
            public int cyBottomHeight;   // height of bottom border that retains its size
        };


        [DllImport("DwmApi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(
            IntPtr hwnd,
            ref MARGINS pMarInset);
    }
    class TransparancyConverter
    {
        private readonly Window _window;

        public TransparancyConverter(Window window)
        {
            _window = window;
        }

        public void MakeTransparent()
        {
            var mainWindowPtr = new WindowInteropHelper(_window).Handle;
            var mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
            if (mainWindowSrc != null)
                if (mainWindowSrc.CompositionTarget != null)
                    mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);

            var margins = new Margins
            {
                cxLeftWidth = 0,
                cxRightWidth = Convert.ToInt32(_window.Width) * Convert.ToInt32(_window.Width),
                cyTopHeight = 0,
                cyBottomHeight = Convert.ToInt32(_window.Height) * Convert.ToInt32(_window.Height)
            };

            if (mainWindowSrc != null) DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct Margins
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        }

        [DllImport("DwmApi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref Margins pMarInset);
    }
}
</code></pre>

<p>In addition, I have tried setting basic transparency in the code behind WITHOUT keeping the border. One will also find my xaml and code behind for this below. The created window is not transparent and just has a white background. What am I doing wrong in both of these examples? I feel like there is a common thread.</p>

<pre><code>&lt;Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        x:Name="MainWindowGUI"
        &gt;
    &lt;Grid&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>

<p>Code Behind</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication10
{
    /// &lt;summary&gt;
    /// Interaction logic for MainWindow.xaml
    /// &lt;/summary&gt;
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainView_Loaded;


        }

        void MainView_Loaded(object sender, RoutedEventArgs e)
        {
            Window yourParentWindow = Window.GetWindow(MainWindowGUI);
            yourParentWindow.WindowStyle = WindowStyle.None;
            yourParentWindow.AllowsTransparency = true;
            SolidColorBrush Transparent = new SolidColorBrush(System.Windows.Media.Colors.Transparent);
            yourParentWindow.Background = Transparent;
        }
    }
}
</code></pre>