views:

92

answers:

2

Hey SO, so I've got a custom control in Silverlight. Doesn't really matter what it is but its a big composite thing with a treeview, a slider, and some other junk.

Now in the control we have an instance variable which is called defaultTemplate. It is of type DataTemplate. Basically its the template that the TreeViewItem's use as their header template unless the application developer calls myControl.setTemplate(DataTemplate dt);

Just by making the property public and giving it getters and setters, it shows up in intellisense when I type the following...

<myNamespace:myControl x:Name="theControl" defa

Intellisense picks up on the fact that my class has a defaultTemplate property and it lists it as an option.

What I would like:

I would like for the above functionality to continue, but add functionality for intellisense to automatically list options for the values. So I have these data templates defined in a resource dictionary which the control has access to. I want them to popup as options when I get to this part of typing:

<myNamespace:myControl x:Name="theControl" defaultTemplate="_"

The functionality I'm, looking for can be found in the TextBlock's foreground property. As you type

foreground="" its start to list colors for you to choose from. I would be fine with defining constants as the names of my data templates and having intellisense choose from those or something like that.

How can I acheive this? Thank you!


Clarification Edit: I need to make intellisense aware of a list of constants which are acceptable inputs for the xaml property. So my defaultTemplate property can just be a string. I'll deal with what that string means in my code behind. But I need intellisense to know what strings are ok to enter for that property in the XAML. The foreground color once you type the quote pops up a list in intellisense that is like "Azure, Beige... LemonChiffon... etc." How do I provide intellisense with that list? Thanks!


Final Edit: Thanks to siege898 I can now choice my values for defaultTemplate from a list that intellisense provides me. However, I would also like to be able to give the application developer (who uses my control) an option to define their own data template and specify the name of it.

So for example, Bob is using my control in his application. None of the templates I provide are suitable for him. So Bob creates in a resource dictionary a data template for his application called bobTemplate. I would like for Bob to be able to enter in the defaultTemplate field in xaml defaultTemplate="bobTemplate" and not have that throw an error. I believe the problem I'm having now is because the field is expecting a value in my enum. Does anyone know how I can set this up to both give me the dropdown list generated from the enum, but still accept custom strings? I was thinking a type converter, but I'm not sure how to use it for what I want...

+3  A: 

Unless you are exposing an enumeration, which intellisense will pick up from properties and dependency properties that are defined as enumerations, you will need to supply a type converter. This is what provides the XAML parser with the "instructions" to parse strings into other types.

Take a look here for background: http://msdn.microsoft.com/en-us/library/cc645047(VS.95).aspx

Then take a look here for a tutorial: http://umairsaeed.com/2010/02/03/creating-a-custom-xaml-typeconverter/

In the case of foreground, for example, the property is mapped to the static Colors class that supplies the list of values that are represented in the XAML.

Jeremy Likness
Hm, ok but what I'm really looking for I think is the first part of your post. How do I provide intellisense with knowledge of the choices for values? So in the foreground example, how do I do the mapping to the list of colors that pops up? Thanks
JGord
+4  A: 

I think what you're working for is an enumerated property. So here's what I suggest:

public enum Templates
{
    Template1, Template2, ...
}

public Templates defaultTemplates
{
   get;
   set;
}

However, because its an enumerated property, when the user passes a value (for instance Template1), the app will see that as a integer (0 = Template1, 1 = Template2...)

So I also suggest keeping some sort of dictionary, or having a lookup function that maps each integer to the template you want. (Or even a type converter as mentioned above)

I'm not 100% sure why this adds to intellisense, but in my experience it always has, even in XAML. I think that VS sees that defaultTemplates must be set to one of the values in Templates so it then populates the list.

Hope this is what you're looking for.

Siege898
Thanks, worked! Anyone know how intellisense becomes aware of the enum values?
JGord