开发者

How can I pass a type as a converter parameter in Silverlight?

开发者 https://www.devze.com 2023-03-12 10:45 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消