views:

173

answers:

1

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.

  1. Main.xaml contains a map control.
  2. 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.

+1  A: 

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 MyMap in this example):-

public MyMap Map
{
    get { return (MyMap)GetValue(MapProperty); }
    set { SetValue(MapProperty, value); }
}

public static DependencyPropery MapProperty = new DependencyProperty("Map",
    typeof(MyMap), typeof(MapEditor), new PropertyMetaData(null));

Now in Blend the Map property 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 ContentControl base class and include a ContentPresenter at the heart of the template. The control would make the assumption that the child control is a MyMap control.

This approach allows the entire appearance of the MapEditor control to be styled in Blend and it allows the Map control that is to be "edited" to be drap-drop onto the MapEditor as a child control.

AnthonyWJones
In the last line, what is MyControl in the function typeof(MyControl)?
Shawn Mclean
@Shawn: Sorry, I should've put `MapEditor` in there to make it clear. In this parameter you specify the type of the control with which the dependency property is associated. This allows different controls to register properties of the same name (without this only one control could ever have a "Text" property, that would never do). I'll edit.
AnthonyWJones
Hello, another question, what does the xaml look like for the new control with the Map property in terms of passing the map object to it?
Shawn Mclean
@Shawn: Thats a good question in its own right, however since the Map control already exists in you XAML elsewhere I would guess you want to bind to it. Lets assume your `Map` is has `x:Name="MyMap"`. Hence your `Map` property would look like `Map="{Binding ElementName=MyMap}"`
AnthonyWJones