Chinjoo
Chinjoo

Reputation: 2792

How to bind data from Static class to Xaml

I have a static class in which one property defines a resource manager. for eg.

public static class MyClass
{
public static MyResourceManager {get;set;}
}

I want to bind the key from the resource manager to XAML. I am using Silverlight 4 with MVVM pattern.

If I do something like this:

<Button Content="{Binding LocalResource.Refresh}"/>

where LocalResource is defined in my view model like this:

public object LocalResource {
get
{
return MyClass.MyResourceManager;
}
}

Can anyone suggest a better approach. I have made the class static as the resource file is loaded dynamically. I am loading it in the bootsrapper and have to use this across all my modules.

Upvotes: 2

Views: 3784

Answers (1)

bitbonk
bitbonk

Reputation: 49619

How about instanciating your resourcemanager just once for your application like this:

<Application xmlns:myNS="clr-namespace:MyNamespace;assembly=MyAssembly" ...>
    <Application.Resources>
        <myNS:MyResourceManager x:Key="MyResources" />
    </Application.Resources>
</Application>

Then you can use it just like a normal WPF resource:

<Button Content="{Binding Source={StaticResource MyResources}, Path=Refresh}" />

Upvotes: 2

Related Questions