Oscar
Oscar

Reputation: 11

WPF usercontrol and external events

I'm a rookie in c# and I'm working on a player with an specialized card. I've work on a usercontrol with the typical buttons stop play, pause and record. The problem is that I don't know how to create the events such that from the principal code (Window) I could manage and call the correspondent function, since from the usercontrol file I can't acces any of the items (of course defined in window) I need to make the things work, as they are not accesible.

Thanks in advance

Best regards,

Oscar

Upvotes: 1

Views: 917

Answers (1)

itowlson
itowlson

Reputation: 74802

Your UserControl needs to expose appropriate properties, methods and events, so that your Window can interact with it. For example, your UserControl might declare a Paused event, so that the Window could respond when the user pauses the control; or it might declare a Play method, so that the Window can start the control playing. When you instantiate your UserControl in the Window's XAML file, give it a name, e.g.

<local:MyControl x:Name="myControl" />

You can then refer to it from code-behind, e.g.:

myControl.Paused += MyControl_Paused;
myControl.Play();

To create the required API, create public properties, methods and events on the UserControl in the code-behind class. You say you are a "rookie in C#" so I don't know how much guidance you need, but unless you need to get into WPF data binding or event routing, you can use normal C# / .NET properties, methods and events for this. Declaring properties, methods and events in C# is widely covered in MSDN and the literature. If you specifically need help working with WPF data binding or event routing, leave a comment outlining your specific difficulty and I'll update the answer.

Upvotes: 1

Related Questions