I've been trying to change the opacity of my window through code for a while now but I've had no success. I'm changing the Window.Opacity
number in my function but I've had no results. As of now, I've tried setting the background to a SolidBrushColor
and setting the opacity from there but nothing happens. I'm also trying INotifyPropertyChanged
still to no avail. There are no errors either. The opacity just isn't affected. Here is the code of the overlay window that needs its opacity changed:
public partial class overlay : Window, INotifyPropertyChanged
{
public static overlay? thisOverlay;
DoubleAnimation fade = new DoubleAnimation(0, new Duration(TimeSpan.FromSeconds(5)));
public event PropertyChangedEventHandler? PropertyChanged;
private double _windowOpacity;
public double WindowOpacity
{
get => _windowOpacity;
set
{
if (_windowOpacity == value)
return;
_windowOpacity = value;
OnPropertyChanged();
}
}
public overlay()
{
InitializeComponent();
this.Loaded += Overlay_Loaded;
WindowOpacity = 0.2;
}
private void Overlay_Loaded(object sender, RoutedEventArgs e)
{
thisOverlay = this;
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public void setOpacity() // Function is ran outside of this class
{
this.WindowOpacity = 0.5; // This doesn't work
Trace.WriteLine("Works"); // This prints
}
and here is the XAML code for the window:
<Window x:Class="NAME.overlay"
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"
xmlns:local="clr-namespace:NAME"
mc:Ignorable="d"
Title="overlay" Height="450" Width="800" WindowState="Maximized" WindowStyle="None" Topmost="True" AllowsTransparency="True" Opacity="{Binding WindowOpacity}" Visibility="Hidden">
</Window>