Have you ever noticed how hard it is to search for information about "Type". It's such a generic concept...
Anyway...does anyone know how to specify a DependencyProperty of type "Type", and then specify the value for that in Xaml in Silverlight? For example, I have a dependency property that looks like this:
public
Type EntityType {
get {
return (Type)GetValue(EntityTypeProperty); }
set { SetValue(EntityTypeProperty,
value); }
}
public
static
readonly
DependencyProperty EntityTypeProperty =
DependencyProperty.Register("EntityType",
typeof(Type),
typeof(DataGridTableEditor),
null);
If I were using WPF, I would do something like this in Xaml to specify an EntityType of "BTParamter":
That doesn't work in Silverlight because x:Type isn't supported. When specifying a TargetType for a Style, I can simply use syntax like TargetType="local:DataGridTableEditor". However, if I
try to do this:
I would suspect I need some sort of automatic conversion from string to type, but I'm not seeing any obvious conversion going on when I look at System.Windows.Style in Reflector.
Another quick note: it looks like MarkupExtension isn't supported in Silverlight yet, so I can't create my own version of {x:Type} that might provide the support I need.
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?
I'd like to add some emphasis to this issue. This is a big deal for what I am trying to accomplish. It's embarassing to see that someone from Microsoft hasn't even bothered to answer this question at all in 4 months. Would someone please address this?
I've run into another place where this is an issue. How do you specify TargetType for a Style or a ControlTemplate for a custom control subclass you've created? I've got a Button subclass, and I want to use TemplateBinding in a ControlTemplate to bind
to a property of the subclass. Without being able to specify TargetType on the ControlTemplate, TemplateBinding fails on initialization. Here's some basic code:
public class MyButton: Button {
public string MyString {
get { return (string)GetValue(MyStringProperty); }
set { SetValue(MyStringProperty, value); }
}
public static readonly DependencyProperty MyStringProperty =
DependencyProperty.Register("MyString", typeof(string), typeof(MyButton), new PropertyMetadata(null));
}
Yes. The TargetType properties of Templates and Styles are special. The Xaml parser is basically hardcoded to understand how to convert string values to types in this instance. It is a special case and you cannot use types elsewhere in Silverlight Xaml.
Have you ever noticed how hard it is to search for information about "Type". It's such a generic concept...
Anyway...does anyone know how to specify a DependencyProperty of type "Type", and then specify the value for that in Xaml in Silverlight? For example, I have a dependency property that looks like this:
public
Type EntityType {
get {
return (Type)GetValue(EntityTypeProperty); }
set { SetValue(EntityTypeProperty,
value); }
}
public
static
readonly
DependencyProperty EntityTypeProperty =
DependencyProperty.Register("EntityType",
typeof(Type),
typeof(DataGridTableEditor),
null);
If I were using WPF, I would do something like this in Xaml to specify an EntityType of "BTParamter":
That doesn't work in Silverlight because x:Type isn't supported. When specifying a TargetType for a Style, I can simply use syntax like TargetType="local:DataGridTableEditor". However, if I
try to do this:
I would suspect I need some sort of automatic conversion from string to type, but I'm not seeing any obvious conversion going on when I look at System.Windows.Style in Reflector.
xiard
Member
77 Points
46 Posts
Creating a DependencyProperty of type "Type"
Jun 10, 2008 04:12 PM | LINK
Have you ever noticed how hard it is to search for information about "Type". It's such a generic concept...
Anyway...does anyone know how to specify a DependencyProperty of type "Type", and then specify the value for that in Xaml in Silverlight? For example, I have a dependency property that looks like this:
public Type EntityType {
public static readonly DependencyProperty EntityTypeProperty = DependencyProperty.Register("EntityType", typeof(Type), typeof(DataGridTableEditor), null);get { return (Type)GetValue(EntityTypeProperty); }
set { SetValue(EntityTypeProperty, value); }
}
If I were using WPF, I would do something like this in Xaml to specify an EntityType of "BTParamter":
<
local:DataGridTableEditor EntityType="{x:Type local:BTParamter}"/>That doesn't work in Silverlight because x:Type isn't supported. When specifying a TargetType for a Style, I can simply use syntax like TargetType="local:DataGridTableEditor". However, if I try to do this:
<
local:DataGridTableEditor EntityType="local:BTParamter"/>I get an exception when I launch:
XamlParseException occurred: AG_E_PARSER_BAD_PROPERTY_VALUE
I would suspect I need some sort of automatic conversion from string to type, but I'm not seeing any obvious conversion going on when I look at System.Windows.Style in Reflector.
Any thoughts?
Thanks,
David
xiard
Member
77 Points
46 Posts
Re: Creating a DependencyProperty of type "Type"
Jun 10, 2008 04:14 PM | LINK
Another quick note: it looks like MarkupExtension isn't supported in Silverlight yet, so I can't create my own version of {x:Type} that might provide the support I need.
xiard
Member
77 Points
46 Posts
Re: Creating a DependencyProperty of type "Type"
Jun 10, 2008 04:50 PM | LINK
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
rob.eisenberg
Member
27 Points
16 Posts
Re: Creating a DependencyProperty of type "Type"
Oct 03, 2008 04:39 PM | LINK
I'd like to add some emphasis to this issue. This is a big deal for what I am trying to accomplish. It's embarassing to see that someone from Microsoft hasn't even bothered to answer this question at all in 4 months. Would someone please address this?
xiard
Member
77 Points
46 Posts
Re: Creating a DependencyProperty of type "Type"
Oct 24, 2008 05:47 AM | LINK
I've run into another place where this is an issue. How do you specify TargetType for a Style or a ControlTemplate for a custom control subclass you've created? I've got a Button subclass, and I want to use TemplateBinding in a ControlTemplate to bind to a property of the subclass. Without being able to specify TargetType on the ControlTemplate, TemplateBinding fails on initialization. Here's some basic code:
Of course, that fails because x:Type doesn't exist.
David
xiard
Member
77 Points
46 Posts
Re: Creating a DependencyProperty of type "Type"
Oct 24, 2008 07:10 AM | LINK
It turns out you don't need x:Type at all in my case above. This is the correct Xaml:
David
rob.eisenberg
Member
27 Points
16 Posts
Re: Creating a DependencyProperty of type "Type"
Oct 24, 2008 01:43 PM | LINK
Yes. The TargetType properties of Templates and Styles are special. The Xaml parser is basically hardcoded to understand how to convert string values to types in this instance. It is a special case and you cannot use types elsewhere in Silverlight Xaml.
mamadero2
Member
58 Points
38 Posts
Re: Creating a DependencyProperty of type "Type"
Jan 27, 2009 04:36 AM | LINK
It's a shame this is not supported and we don't have a way to do it ourselves. According to this article, this is not supported:
http://msdn.microsoft.com/en-us/library/cc917841(VS.95).aspx
I think your solution is the best we can do at the moment.
Readify Consulting
www.miguelmadero.com
SL Tip of The Day (http://shrinkster.com/10tp)
(Please click on "Mark As Answer", if this has answered your query. Thanks.)
murtaza.dhar...
Participant
1659 Points
378 Posts
Re: Creating a DependencyProperty of type "Type"
Jan 27, 2009 05:23 AM | LINK
can you try something like this
in the Resources Create an Object of
<local:BTParamter x:key="MyParam" />
then in xaml you try this
<local:DataGridTableEditor EntityType="{StaticResource MyParam}"/>
Diaspark Inc.
www.diaspark.com
email:murtaza.dharwala@diaspark.com
Please remember to click “Mark as Answer” on the post that helps you
mamadero2
Member
58 Points
38 Posts
Re: Creating a DependencyProperty of type "Type"
Jan 27, 2009 12:03 PM | LINK
That would create an object of type BTParameter instead of getting the Type.
Readify Consulting
www.miguelmadero.com
SL Tip of The Day (http://shrinkster.com/10tp)
(Please click on "Mark As Answer", if this has answered your query. Thanks.)