I’m creating a silverlight user control that I should be able to drag and drop via blend. But this control needs to accept a map that is already on the page.
For eg.
- Main.xaml contains a map control.
- MapEditor.xaml contains buttons and other controls. In the .cs file, it needs to access a map control (the one in Main.xaml).
How do I go about getting this done?
I was thinking about adding a parameter in the contructor for MapEditor but how would I pass in the map as a parameter in design mode?
Thanks.
ps. I’m going to break out this control into a silverlight library so it could be used in multiple projects later.
You don’t want to be giving your control a parameterised constructor, XAML will only construct types using their default constructor.
Simple Approach
The easiest approach would be to add DependencyProperty to your control to which you would assign the Map control (I’ll use the type name
MyMapin this example):-Now in Blend the
Mapproperty will appear in the Miscellaneous category in the Properties tab. You can then use the “Element Property” tab of the “Create Data Binding” to select the Map control to which it should bind.Hard Core Approach
That said I would be inclined to build a proper customisable control following these guidelines Creating a New Control by Creating a ControlTemplate. With the addition that I would extend the
ContentControlbase class and include aContentPresenterat the heart of the template. The control would make the assumption that the child control is aMyMapcontrol.This approach allows the entire appearance of the
MapEditorcontrol to be styled in Blend and it allows the Map control that is to be “edited” to be drap-drop onto theMapEditoras a child control.