Skip to content

Instantly share code, notes, and snippets.

@aurimasniekis
Last active June 6, 2021 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurimasniekis/53533fddefe95b04bcad37b815197b8e to your computer and use it in GitHub Desktop.
Save aurimasniekis/53533fddefe95b04bcad37b815197b8e to your computer and use it in GitHub Desktop.
How to allow WPF custom window element to have transparency set from style

How to allow WPF custom window element to have transparency set from style

While developing my one application I encountered situation where my custom WPF Window class which I style from ResourceDirectory Type based style and try to set AllowsTransparency fails with Exception Cannot change AllowsTransparency after Window has been shown. Here is my quick way to make style set this property at creation (You cant still modify this after shown).

The idea is to override AllowsTransparency property in class so it would not set base Window.AllowsTransparency from style. Then find style using FrameworkElement.TryFindResource method and fetch the setter for AllowsTransparency and check if its set true and then apply base.AllowsTransparency = true and WindowStyle = WindowStyle.None.

I have included my Style extension which allows to search for setter through whole style inheritence tree and return first match.

License

MIT License

Copyright (c) 2021 Aurimas Niekis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

namespace WpfApplication1
{
public class MyWindow : Window
{
public new static readonly DependencyProperty AllowsTransparencyProperty =
DependencyProperty.Register(nameof(AllowsTransparency), typeof(bool), typeof(MyWindow), new PropertyMetadata(false));
public new bool AllowsTransparency
{
get => (bool) GetValue(AllowsTransparencyProperty);
set => SetValue(AllowsTransparencyProperty, value);
}
public MyWindow()
{
if ((bool) (this.FindStyleFirstSetter(typeof(MyWindow), AllowsTransparencyProperty).Value ?? false))
{
base.AllowsTransparency = true;
WindowStyle = WindowStyle.None;
}
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Style x:Key="MyWindow" TargetType="local:MyWindow">
<Setter Property="AllowsTransparency" Value="True"></Setter>
</Style>
<Style x:Key="{x:Type local:MyWindow}" BasedOn="{StaticResource MyWindow}" TargetType="local:MyWindow"/>
</ResourceDictionary>
namespace WpfApplication1
{
public static class StyleExtension
{
public static Setter FindFirstSetter(this Style style, string property)
{
var currentStyle = style;
while (currentStyle != null)
{
if (currentStyle.Setters.FirstOrDefault(x => x is Setter s && s.Property.Name == property) is Setter setter)
{
return setter;
}
currentStyle = currentStyle.BasedOn;
}
return null;
}
public static Setter FindFirstSetter(this Style style, DependencyProperty property) => FindFirstSetter(style, property.Name);
public static Setter[] FindAllSetter(this Style style, string property)
{
var result = new List<Setter>();
var currentStyle = style;
while (currentStyle != null)
{
if (currentStyle.Setters.FirstOrDefault(x => x is Setter s && s.Property.Name == property) is Setter setter)
{
result.Add(setter);
}
currentStyle = currentStyle.BasedOn;
}
return result.ToArray();
}
public static Setter[] FindAllSetter(this Style style, DependencyProperty property) => FindAllSetter(style, property.Name);
public static Setter FindStyleFirstSetter(this FrameworkElement element, object resourceKey, string property) =>
(element.TryFindResource(resourceKey) as Style)?.FindFirstSetter(property);
public static Setter FindStyleFirstSetter(this FrameworkElement element, object resourceKey, DependencyProperty property) =>
FindStyleFirstSetter(element, resourceKey, property.Name);
public static Setter[] FindStyleAllSetter(this FrameworkElement element, object resourceKey, string property) =>
(element.TryFindResource(resourceKey) as Style)?.FindAllSetter(property);
public static Setter[] FindStyleAllSetter(this FrameworkElement element, object resourceKey, DependencyProperty property) =>
FindStyleAllSetter(element, resourceKey, property.Name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment