© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
424 replies
Exilon

Window opacity not being effected in a method?

Hello, 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
Window.Opacity
number in my function but I've had no success. As of now, I've tried setting the background to a
SolidBrushColor
SolidBrushColor
and setting the opacity from there to no success. I'm also trying
INotifyPropertyChanged
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
        }
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
        }


Tell me if you would like to see any XAML code or MainWindow.cs.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

Window opacity isn't being affected?
C#CC# / help
4y ago
✅ Issues with method not being available
C#CC# / help
3y ago
Background Opacity in WinFroms
C#CC# / help
4y ago
Chaining async methods. Method not found
C#CC# / help
3y ago