Is it possible to data bind to a method in Silverlight?
We have a dynamic languaging system that doesn't use conventional resource files; fetching resources from a database, via a language manager library instead. Based on a legacy (VB6) solution, it languages controls according to the resource name in the control's tag property. After the page has initialised we fire off a method that takes LayoutRoot and recurses it's children applying strings looked up from 开发者_如何学Cthe tag where the control has one. This proves to be unwieldy, the recursive routine has turned into a monster to cater for the subtleties of different controls and I want to change to using data binding to apply the strings instead.
I realise I could declare properties to bind to for each control but this will involve a lot of code and I'm hoping there is a better way. Is there some way to bind to a method, passing a parameter. I envisage a method something like this.
public string GetResource(string resourceName)
{
string resource = <fetch resource from language manager>;
if (String.IsNullOrEmpty(resource))
{
return resourceName;
}
else
{
return resource;
}
}
We already have a static method in our application for this retrieval, which has the following signature.
public static Resource(string resourceName, string defaultValue)
To be able to use that directly would be great.
You could use a value converter and then pass a parameter to that for looking up the resources. It wouldn't necessarily matter what property the binding went to as your value converter could ignore that part and just use the passed ConverterParameter
to perform your resource look-up.
You can then create an instance of your resource converter in the App resources and use it throughout your application.
... SomeProperty="{Binding Converter={StaticResource MyAppResourceConverter},
ConverterParameter=SomePropertyResourceName}}" ...
精彩评论