Databind Resource File in XAML

前端 未结 2 899
Happy的楠姐
Happy的楠姐 2021-01-17 18:31

For localization I\'m using the Resource-file (.resx files) functionality in .NET, but I\'m wondering if there\'s a smart way to databind the various localization properties

2条回答
  •  伪装坚强ぢ
    2021-01-17 19:02

    Here is how I do it.

    WPF:

    1. Create a resource file and in the same assembly create a class that has a public constructor. Make sure the resource file is marked public.

    2. In your xaml file - add a reference to this location in the namespaces

      xmlns:res="clr-namespace:MyProject.StringResources"

    3. For your text property use the following binding

      TextProperty="{x:Static res:ResourceFileName.ResourceKey}"

    Silverlight:

    1. Follow steps 1 & 2 above and then add the resource file as a Resource in either your user control or in an application level resource:

    2. For your text property use the following binding:

      TextProperty="{Binding ResourceKey, Source={StaticResource resourceLabels}}"

提交回复
热议问题