次の方法で共有


How to: Make Non-Dependency Properties Localizable

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

Silverlight does not support data binding for properties that have the following characteristics:

  • The property is not a dependency property.

  • The property belongs to an object that is not a DependencyObject object.

In these cases, if you want to localize a property value, you must write code to connect the property to the appropriate resource.

The following procedure assumes 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.

NoteNote:

In addition to using this technique to retrieve a string resource directly from a resource file, you can also use the {StaticResource} markup extension to make a non-dependency property localizable. For more information, see How to: Make Properties Localizable with Static Resources.

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. Add an x:Name attribute to the control, so you can refer to the property that you would like to localize in code.

    <data:DataGridTextColumn x:Name="column"
                             Header="Text to be localized"/>
    
  3. Remove the string that you want to localize from the XAML code.

    <data:DataGridTextColumn x:Name="column" />
    
  4. Add the string that you want to make localizable to your resource file. In this case, name it columnHeader, and assign it the string "Text to be localized".

  5. In your code-behind file, add the following code to the class constructor after the call to the InitializeComponent method.

    column.Header = My.Resources.Resource1.columnHeader
    
    column.Header = SilverlightApp.Resource1.columnHeader;