I hope the title is somewhat clear.
In windows forms this is pretty straight forward.

But for whatever reason in WPF its not the case.

I am trying to change the opactiy of 2 labels.
Both labels are on seperate windows.
Window1 has a button to change opacity of a label to 0 and a label to change it to 100.
But these buttons need to change both labels on window1 and window2.

Is this possible in WPF?

Got this as code for the opacity, which only works on the form where the button is on.

        private void BtnIncreaseOpacity_Click(object sender, RoutedEventArgs e)
        {
            lblDrag.Opacity = 100;
            win2.lblDrag2.Opacity = 100;
        }

In the Public partial class

        TrackerMessage win2 = new TrackerMessage();

Got the same problem with getting a label to become visible when a timer on window1 has run out.

Any help is appreciated.
Thanks

Dani AI

Generated

Two things are most likely going wrong here: the value you set for WPF Opacity, and which window instance you are changing. In WPF Opacity is a double from 0.0 (transparent) to 1.0 (opaque) — using 0/100 like WinForms will not behave as you expect. Also, if you create a new TrackerMessage instance in code you will not affect the window already shown; you must update the same instance or share state between both windows. If your timer is a non-UI timer, updates must be marshalled to the UI thread via Dispatcher.

A robust WPF way is to bind both labels to a shared ViewModel property. Example (very small) ViewModel and XAML binding:

public class SharedViewModel : INotifyPropertyChanged {
    private double _labelOpacity = 1.0;
    public double LabelOpacity {
        get => _labelOpacity;
        set {
            if (_labelOpacity == value) return;
            _labelOpacity = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(LabelOpacity)));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
<!-- in each window's XAML: -->
<Label Content="status" Opacity="{Binding LabelOpacity}" />

Create one ViewModel and give it to both windows (set DataContext). Changing the ViewModel property updates both labels automatically.

If you prefer a direct approach, keep and use the actual window reference you showed (do not instantiate a new one), or locate the open window:

var tracker = Application.Current.Windows
               .OfType<TrackerMessage>()
               .FirstOrDefault();
tracker?.Dispatcher.Invoke(() => tracker.SetLabelOpacity(0.0));

Expose a small method on TrackerMessage that sets its label safely using Dispatcher.Invoke if needed.

's event/delegate idea is valid too: publish an opacity change event and have the other window subscribe (use Dispatcher inside the subscriber). For quick fixes use a shared ViewModel or a stored window reference; for cleaner WPF code prefer binding + INotifyPropertyChanged (MVVM).

Recommended Answers

All 4 Replies

I haven't worked with WPF, but can't you just use an eventhandler (and a delegate)?

Tried that also with no success.

thanks going to read through that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.