I'm not positive that this is the best approach, but it is one approach. I created a TypeConverter subclass and applied it to my EntityType property:
public
class
StringToTypeConverter :
TypeConverter {
public
override
bool CanConvertFrom(Type sourceType) {
return sourceType.IsAssignableFrom(typeof(string));
}
public override
object ConvertFrom(object value) {
string text = value
as
string;
if (text !=
null) {
return
Type.GetType(text);
}
return
null;
}
}
[TypeConverter(typeof(StringToTypeConverter))]
public
Type EntityType {
get {
return (Type)GetValue(EntityTypeProperty); }
set { SetValue(EntityTypeProperty,
value); }
}
I'm not fond of this approach for a couple of reasons. First, it means I need to add the TypeConverter attribute to every property where I need this conversion functionality rather than using a core framework feature like x:Type.
Secondly, it would only work with types accessible from within my currently executing assembly, whereas I'm sure x:Type is more intelligently/flexibly implemented using a MarkupExtension which allows you to use the Xml namespaces defined at the top of the
Xaml file. Thirdly, I'm reimplementing code (a poor man's version of it, anyway) which is obviously contained within the Silverlight framework somewhere. However, it does get me past that current hurdle.
The original question still stands, though. Is there a built-in way to do this that I'm missing?
David
I use the same approach but I've just faced one interesting issue. My converter accepts these syntaxes: Double, System.Double, System:Double
The problem is that the Blend doesn't use my converter and accepts only this syntax: System:Double.
Maybe I'll put a restriction and only System:Dobule syntax will be allowed.
Dochev
Member
2 Points
1 Post
Re: Creating a DependencyProperty of type "Type"
Jun 21, 2009 06:20 PM | LINK
I use the same approach but I've just faced one interesting issue. My converter accepts these syntaxes: Double, System.Double, System:Double
The problem is that the Blend doesn't use my converter and accepts only this syntax: System:Double.
Maybe I'll put a restriction and only System:Dobule syntax will be allowed.