Getting the id of the hosting WPF/E control
Hi,
The new CTP contains the GetHost method but how can I get the id of the hosting control?
Lets say I have 2 WPF/E controls on the same HTML implementing the same click event. I need to identify which control was clicked.
Thanks,
Keren
[267 byte] By [
KerenF] at [2007-12-31]
There doesn't appear to be any way to get this in the current CTP. I would suggest you use either the Source property of the control to determine this or give your elements names that you can use to distinguish what was click (ex: x:Name="Ctrl1Rect1").
Maybe one of the MS people will respond with whether this is something they are going to add in a future release...
Let me make sure I understand the issue. You create a couple instances of WPF/E controls on a page, each with a unique id. I'm assuming you do this via the agHost function or directly via OBJECT/EMBED tags. In each case you've set the id. Any time later, you should be able to use document.getElementById(<id_you_created_the_object_with>) to get the control instance. Is this not working?
Update: Posted too soon. I also meant to add: What do you intend to do with the id that you cannot do with sender.getHost(); directly?
What I meant is as following:
Lets say I have 2 WPF/E controls that refer to the same XAML so actually I distnguish between them with their Id. In the XAML I call an "OnClick" method which in its implementation I want to invoke one behavior for the first control and another behavior for the second control. Therefore I would like to be able to do something like this in my OnClick function:
if (sender.getHost().id == "ctrl1")
...
else //id is "ctrl2"
...
Thinking of workarounds here. It seems theoretically possible to have a unique element name in each XAML file for each plug-in in question. Say, the root element in each file has a unique name. Then in your event handler body you can do a if(sender.FindName(...)) { // do something }.
Personally, I couldnt' figure it out. However, I think I figured out a viable workaround. The workaround is as follows:
I used the "SourceElement" property of the WPF/E Control to store the id of the hosting control. This approach is probably not optimal, however, because the SourceString and Source property values take precedence in loading XAML content, the approach has worked fine for me. In addition, the "SourceElement" property is just a string and it doesn't seem to require XAML. I'm sure it doesn't work in 100% of the cases, however, it seems to be a viable workaround for the time being.
I have been using this approach within a component I am working on and it has not caused any problems and it has addressed my needs perfectly.
I am constructing a blog post regarding this and I hope to have it finished this evening. In addition, the component I am creating that uses this implementation will be available within the next couple of weeks.
If this answers your question, please select "Mark as Answer", otherwise, please post a followup question.
Thank you,
Chad Campbell