locked
Add Event to Dynamic Xaml RRS feed

  • Question

  • I have some Xaml that is retrieved from a WCF Service. I parse that string and then add it as a child (canvas.children.add(xamlString)) to a canvas. Everything works perfect there no issues at all. 

    The trouble is that some of the elements need to have an event. In this case it is a Path. 

     My question is this how do I add the event (such as MouseEnter) via code behind. I've tried a few things even just hard coded the attribute MouseEnter="MyPath_MouseEnter" as a test but that breaks the Xaml although if I hard code the Xaml (what I would normally load dynamically) on my layout the very same process works just fine and triggers its associated event in code behind.

     So what would be the best way of adding an event to an element (again in this case a path) such as MouseEnter to the dynamically added elments.

     By the way I can added the event as an attribute to the element in code behind no problem. But again when I do that it breaks and the paths never render.

    Thanks in advance as I've been messing with this for a couple hours.

    Saturday, October 10, 2009 6:28 PM

Answers

  •  

    I have some Xaml that is retrieved from a WCF Service. I parse that string and then add it as a child (canvas.children.add(xamlString)) to a canvas. Everything works perfect there no issues at all. 

    After adding all the parsed children, you can add the event like below:

    foreach (var child in myCanvas.Children)
    {
        child.MouseEnter += new MouseEventHandler(child_MouseEnter);
    }

     

    You can filter myCanvas.Children if required.

    Sunday, October 11, 2009 2:10 AM

All replies

  •  

    I have some Xaml that is retrieved from a WCF Service. I parse that string and then add it as a child (canvas.children.add(xamlString)) to a canvas. Everything works perfect there no issues at all. 

    After adding all the parsed children, you can add the event like below:

    foreach (var child in myCanvas.Children)
    {
        child.MouseEnter += new MouseEventHandler(child_MouseEnter);
    }

     

    You can filter myCanvas.Children if required.

    Sunday, October 11, 2009 2:10 AM
  •  yep that is pretty much what I was looking for, going to try it now. post solved if working.

     More of an SQL guy so dealing with XML is not my strongest suit. Wasn't sure of the syntax. thx.

    Sunday, October 11, 2009 3:33 PM