WPF Updating User Control's Control From Main Window

I've got a WPF application that has a Main window with a TabControl in it that I load in different user controls into.  On the Main window I have some login controls and have a public class to hold the CurUserLoggedIn object so I can access it on any of my user controls.  The problem I'm trying to solve now is that based on who is logged in elements of certain user controls contain different information.  If I log in and the user control now has "authenticated user only" content I want to be able to clear this information if the Log Out button is clicked.  What I'm hoping to be able to do is to run some update code within the user control in the Main window's logout code block.  Hopefully this makes sense.  Any help would be greatly appreciated.

Thanks,
Greg
Greg_L_WERAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
Create an EventAggregator and use Pub\Sub. Publish any significant action and any one interested can subscribe to the event and react accordingly.

public class MyEvent
{
	public enum EventNameEnum
	{
		UserAuthenticated
	};

	public EventNameEnum EventName { get; private set; }

	public MyEvent(EventNameEnum eventName)
	{
		EventName = eventName;
	}
}

public static class EventAggregator
{
	private static readonly Dictionary<MyEvent.EventNameEnum, MyEvent> _EventDictionary = new Dictionary<MyEvent.EventNameEnum, MyEvent>();

	private static readonly Dictionary<MyEvent, EventHandler> _EventHandlersByName = new Dictionary<MyEvent, EventHandler>();       

	private static readonly object myLock = new object();

	private static MyEvent GetEvent(MyEvent.EventNameEnum eventName)
	{
		lock (myLock)
		{
			if (!_EventDictionary.ContainsKey(eventName))
			{
				_EventDictionary.Add(eventName, new MyEvent(eventName));
			}

			return _EventDictionary[eventName];
		}
	}

	public static void PublishEvent(MyEvent.EventNameEnum MyEventName, object sender, EventArgs eventArgs)
	{
		PublishEvent(GetEvent(MyEventName), sender, eventArgs);
	}

	public static void PublishEvent(MyEvent MyEvent, object sender, EventArgs eventArgs)
	{
		if (_EventHandlersByName.ContainsKey(MyEvent))
		{
			lock (myLock)
			{
				_EventHandlersByName[MyEvent](sender, eventArgs);
			}
		}
	}       
	

	public static void SubscribeEvent(MyEvent.EventNameEnum MyEventName, EventHandler eventHandler)
	{
		SubscribeEvent(GetEvent(MyEventName), eventHandler);
	}

	public static void SubscribeEvent(MyEvent myEvent, EventHandler eventHandler)
	{
		if (!_EventHandlersByName.ContainsKey(myEvent))
			_EventHandlersByName.Add(myEvent, eventHandler);
	}
	
	public static void RemoveSubscription(MyEvent.EventNameEnum MyEventName)
	{
		if((_EventDictionary!= null) && (_EventDictionary.Keys.Count > 0) && (_EventHandlersByName != null))
		{
			MyEvent MyEvent = GetEvent(MyEventName);

			if ((MyEvent != null) && (_EventHandlersByName.ContainsKey(MyEvent)))
			{
				_EventHandlersByName.Remove(MyEvent);
			}
		}
	}
}

//publish
EventAggregator.PublishEvent(MYEvent.EventNameEnum.UserAuthenticated, this, new EventArgs());

//subscribe
EventAggregator.SubscribeEvent(MyEvent.EventNameEnum.UserAuthenticated, OnUserAuthenticated);

private void OnUserAuthenticated(object sender, EventArgs eventArgs)
{
	//Act here
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Greg_L_WERAuthor Commented:
Thanks SriVaddadi... look forward to giving it a shot.  Will let you know how it goes.

Thanks,
Greg
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.