Prism. EventAggregator to currently active tab

505 views Asked by At

I have this UI shell:
enter image description here

On refresh button, I call _eventAggregator.GetEvent<RefreshEvent>().Publish();

And code in all regions in tabs had subscribed to this event.

Problem: The problem is that I want to update only currently active tab. But in my case all tabs are updating. Is there a good practices to solve this?

Edit 1
I implement IActiveAware in my view:

public partial class ShipsControlView : UserControl, IActiveAware
{
    public ShipsControlView()
    {
        InitializeComponent();
    }

    private bool isActive;

    public bool IsActive
    {
        get
        {
            return this.isActive;
        }
        set
        {
            if (this.isActive == value)
                return;

            this.isActive = value;

            var viewModelActiveAware = this.DataContext as IActiveAware;
            if (viewModelActiveAware != null)
                viewModelActiveAware.IsActive = value;

            this.OnIsActiveChanged();
        }
    }

    public event EventHandler IsActiveChanged;

    protected virtual void OnIsActiveChanged()
    {
        var handler = this.IsActiveChanged;
        if (handler != null)
            handler(this, EventArgs.Empty);
    }
}

Added this view to tabControl:

<TabControl
        prism:RegionManager.RegionName="TabRegion2"
        ItemContainerStyle="{StaticResource HeaderStyle}"
        ></TabControl>

.

regionManager.RegisterViewWithRegion("TabRegion2", typeof(ShipsControlView));

I check the status of IsActive, it is always changed if I click tabs (works right).

Problem: If tab is not active, the commands in that tab triggering on my global composite button, but it must not.

1

There are 1 answers

0
AudioBubble On BEST ANSWER

You shouldn't be using the event aggregator for this. You should be using CompositeCommands. They even monitor active tabs/VMs when they implement IActiveAware automatically.

Here is the docs: http://prismlibrary.readthedocs.io/en/latest/WPF/09-Communication/#solution-commanding