Dynamically created elements' event handler

Ignas Laukineitis

I'm working on a Windows Phone 7.1 (7.5) app. The idea: app gets an list of data from server, creates a TextBlock for each of them and applies Tap event handler for each of them. The problem: since I can only use one handler for all of the elements, how to identify the sender?

The part for creating a new TextBlock: (note: itemsAdded is an outside variable, that is to set proper margins)

void addInfoItem(string text)
    {
        Thickness tempThick = fatherText.Margin;
        tempThick.Top += itemsAdded * 58;
        itemsAdded++;
        TextBlock temp = new TextBlock() { Text = text, FontSize = 40, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Margin = tempThick };
        temp.Tap += whenTapped;
        ContentPanel.Children.Add(temp);
    }

whanTapped event handler:

private void whenTapped(object sender, RoutedEventArgs e)
    {
        //how to identify the sender?
    }

When debugging, "object sender" gives enough info to identify the sender - "Text" property of the TextBlock, but during coding, all i get from "object sender" is the following: Equals, GetHashCode, GetType, ToString. (ToString only tells that this is generally a TextBlock, that's all).

Sudhakar Tillapudi

but during coding, all i get from "object sender" is the following:
Equals, GetHashCode,GetType, ToString.

because Object only supports those methods and it is the parent class of all (almost). and parent class can contain/hold the child class memebrs but you can't access the child members untill unless you cast them to the child type.

so you use can sender object but you need to cast it to your control TextBlock to get the required members to invoke.

TextBlock temp = (TextBlock) sender;
temp.Invi=okeSomething(); //now you can invoke `TextBlock` memebrs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to add an event handler for dynamically created QML elements?

From Dev

Event handler for dynamically created Button

From Java

Event binding on dynamically created elements?

From Dev

adding event handler to a dynamically created button

From Dev

Adding event handler to dynamically added span elements

From Dev

EACH event binding on dynamically created elements

From Dev

Need to pass object through dynamically created event handler

From Dev

Wiring all dynamically-created checkboxes to the same event handler

From Dev

c# event handler for multiple dynamically created control

From Dev

How to add event handler when creating dynamically created view in Android?

From Dev

Event handler invoked multiple times when created dynamically

From Dev

Difficulty attaching event handler to dynamically generated modal window elements

From Dev

bind a click event only once for dynamically created elements

From Dev

Not able to perform click event on dynamically created html elements

From Dev

How to create event listener for elements created dynamically without JQuery

From Dev

jQuery - Event binding on dynamically created elements from iframe

From Dev

Is there a way to apply jQuery to dynamically created elements? (not event listeners)

From Dev

How do I dynamically add an jquery ui event handler to a dynamically created button?

From Dev

Dynamically Created Element and event

From Dev

Styling elements created dynamically

From Dev

getElementByClassName for dynamically created elements

From Dev

Remove a dynamically added event handler

From Dev

Register an event handler for a CSS class using jQuery does not work when elements are added dynamically

From Dev

Custom event args on a dynamically assigned event handler

From Dev

Fire an event for a dynamically created control

From Dev

Event listener for dynamically created html

From Dev

Binding event to dynamically created element

From Dev

onChange event to a list created dynamically

From Dev

attach an event to dynamically created forms

Related Related

  1. 1

    How to add an event handler for dynamically created QML elements?

  2. 2

    Event handler for dynamically created Button

  3. 3

    Event binding on dynamically created elements?

  4. 4

    adding event handler to a dynamically created button

  5. 5

    Adding event handler to dynamically added span elements

  6. 6

    EACH event binding on dynamically created elements

  7. 7

    Need to pass object through dynamically created event handler

  8. 8

    Wiring all dynamically-created checkboxes to the same event handler

  9. 9

    c# event handler for multiple dynamically created control

  10. 10

    How to add event handler when creating dynamically created view in Android?

  11. 11

    Event handler invoked multiple times when created dynamically

  12. 12

    Difficulty attaching event handler to dynamically generated modal window elements

  13. 13

    bind a click event only once for dynamically created elements

  14. 14

    Not able to perform click event on dynamically created html elements

  15. 15

    How to create event listener for elements created dynamically without JQuery

  16. 16

    jQuery - Event binding on dynamically created elements from iframe

  17. 17

    Is there a way to apply jQuery to dynamically created elements? (not event listeners)

  18. 18

    How do I dynamically add an jquery ui event handler to a dynamically created button?

  19. 19

    Dynamically Created Element and event

  20. 20

    Styling elements created dynamically

  21. 21

    getElementByClassName for dynamically created elements

  22. 22

    Remove a dynamically added event handler

  23. 23

    Register an event handler for a CSS class using jQuery does not work when elements are added dynamically

  24. 24

    Custom event args on a dynamically assigned event handler

  25. 25

    Fire an event for a dynamically created control

  26. 26

    Event listener for dynamically created html

  27. 27

    Binding event to dynamically created element

  28. 28

    onChange event to a list created dynamically

  29. 29

    attach an event to dynamically created forms

HotTag

Archive