Share via

How to: Make Properties Localizable with Static Resources

Microsoft Silverlight will reach end of support after October 2021. Learn more.

The {StaticResource} markup extension enables you to retrieve static resources from a resource collection, such as the current application's Application.Resources collection, the application's ResourceDictionary.MergedDictionaries collection, or the page's FrameworkElement.Resources collection. These resources can then be assigned to non-dependency properties, to properties of objects that are not derived from FrameworkElement, and to properties that define a page's or control's style elements.


Because the {StaticResource} markup extension can also be used with dependency properties and objects that are derived from FrameworkElement, it is possible to use it in place of the {Binding} markup extension. However, because this technique adds string resources programmatically, you lose the ability to preview your XAML in Microsoft Expression Blend and Visual Studio Designer.

This topic provides instructions for connecting a non-dependency property or a style element to a resource. It assumes that that you have already created the necessary resource files. (For more information, see Localizing Silverlight-based Applications.) In the case of this example, the resource file is named Resource1.

To make a non-dependency property localizable

  1. Identify the property that you want to make localizable. For example, the following XAML defines a DataGridTextColumn control and sets its Header property.

    <data:DataGridTextColumn Header="Text to be localized"/>
  2. Create a string resource in the Resource1 resource file. Name it columnHeader, and assign it the string "Text to be localized".

  3. Add the following code to the Application.Startup event handler in the App.xaml code-behind file to retrieve the string resource and assign it to a resource named ColumnString in the application's Application.Resources collection.

    Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
       Me.RootVisual = New MainPage()
       App.Current.Resources.Add("ColumnString", My.Resources.Resource1.columnHeader)
       Me.Resources.Add("fontFamily", My.Resources.Resource1.FontFamily)
    End Sub
    private void Application_Startup(object sender, StartupEventArgs e)
       this.RootVisual = new MainPage();
       App.Current.Resources.Add("ColumnString", SilverlightApp.Resource1.columnHeader);
       this.Resources.Add("fontFamily", SilverlightApp.Resource1.FontFamily);

    Note that the resource has to be added to the Application.Resources collection, and not to the Resources collection of the Page class, so that the resources are present before the page's InitializeComponent method is called. You can also add the resources to the MergedDictionaries collection by using the Application.Resources.MergedDictionary property.

  4. Use the {StaticResource} markup extension to connect the Header property of the DataGridTextColumn control to the ColumnString resource:

    <data:DataGridTextColumn Header="{StaticResource ColumnString}"/>

To make a style element localizable

  1. Create a string resource in the Resource1 resource file and assign it any valid name. For example, to assign a value to a control's FontFamily property, you can name the resource FontFamily.

  2. Add the following code to the Application.Startup event handler in the App.xaml code-behind file. This code retrieves the string resource and assigns it to a resource named fontFamily in the application's Application.Resources collection.

    Me.Resources.Add("fontFamily", My.Resources.Resource1.FontFamily)
    this.Resources.Add("fontFamily", SilverlightApp.Resource1.FontFamily);
  3. Use the {StaticResource} markup extension to connect the FontFamily property to the fontFamily resource in the application's Resources collection.

    <Setter Property="FontFamily" Value="{StaticResource fontFamily}"/>