Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help, i need this to know for my school exam next week!!!

I am creating an Email Client in WPF, so far i have a List<> with messages, for each message is created a user control with a unique tag(1,2,3,..) and data inside the form. The user controls are added dynamically in a wrap panel depends on the number of messages.

Something like this:
http://wscont2.apps.microsoft.com/winstore/1x/d1b21d52-3c46-4eef-b118-c30109053170/Screenshot.287867.1000003.jpg[^]

I want to raise an event on the user control, for example open a bigger window with the data from the user control.

How should i raise the event, should i create somekind of custom event, becose i need to compare the tag of the usercontrol created in main window so it matches and opens the correct window?
Posted
Updated 27-Nov-14 12:43pm
v2

1 solution

It would likely help if you posted the code you have for generating the dynamic controls; however, wiring up the events is pretty simple.

For example you can do something like this:

C#
MyUserControl ctl = new MyUserContro();
ctl.MouseRaisedEvent += new ...


and in your user control you have properties to expose the events. An example is at http://stackoverflow.com/questions/6192739/how-do-i-raise-an-event-in-a-usercontrol-and-catch-it-in-mainpage[^]
 
Share this answer
 
Comments
BiggiSmalls 28-Nov-14 7:02am    
This is the code for dynamically generating user controls:

//Message is the class with properties and List<> listofMessages
foreach (Message item in Message.listofMessages)
{
UserCKocka novUc = new UserCKocka();
novUc.Margin = new Thickness(5);
novUc.Width = 180;
novUc.Height = 180;

//Increase tag number
stTaga++;
//Tag user control with the number stTaga
novUc.Tag = stTaga;


//Adding the datas and image for the newly created user control
Uri slikaPot = new Uri(item.Slika, UriKind.RelativeOrAbsolute);
BitmapImage slika = new BitmapImage(slikaPot);
novUc.slikaKocka.Source = slika;

//Textblock pošiljatelj
novUc.txtPosiljateljKocka.Text = item.Od;
novUc.txtZadevaKocka.Text = item.Zadeva;
novUc.SporociloKocka.Text = item.Vsebina;


wrap.Children.Add(novUc);
}

Thank you for helping me :)

Can you help me little more? How do check which user control is clicked by its Tag, in case if i wanted to delete it??
ZurdoDev 28-Nov-14 9:29am    
In the raised event you can check the tag property.
BiggiSmalls 28-Nov-14 13:41pm    
Thank you very much :) i figured it out. Have a nice day
ZurdoDev 28-Nov-14 14:02pm    
Glad to hear it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900