How can I pass a type as a converter parameter in Silverlight?
Let's say I have a button whose IsEnabled property uses a binding, which checks if the button's Da开发者_运维问答taContext is of an expected type. If the DataContext matches the type, the button is enabled; otherwise, it's disabled.
In WPF, I can do this as follows:
IsEnabled="{Binding Converter={StaticResource isObjectOfTypeConverter}, ConverterParameter={x:Type Script:AstScriptProjectViewModel}}"
How can I do this in Silverlight, where x:Type isn't available?
Thanks,
-Craig
I ended up solving this problem by changing my converter parameter to use a string, which I convert to a type within the converter. For instance, my binding is now as follows:
IsEnabled="{Binding Converter={StaticResource isObjectOfTypeConverter}, ConverterParameter='Project.Script.AstScriptProjectViewModel'}"
Within the converter, I use GetType to turn the parameter string into a Type:
var typeString = parameter as string;
if (!string.IsNullOrWhiteSpace(typeString))
{
type = Type.GetType(typeString);
}
From what I can tell, x:Type can't be used as a static value in Silverlight.
精彩评论