C#C
C#4y ago
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 number in my function but I've had no success. As of now, I've tried setting the background to a SolidBrushColor and setting the opacity from there to no success. 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
        }


Tell me if you would like to see any XAML code or MainWindow.cs.
Was this page helpful?