[ACCEPTED]-How to fetch string from resource to assign in WPF Resource section in xaml-xbap

Accepted answer
Score: 49

I was able to do it in a program with:

<TextBlock VerticalAlignment="Center" Margin="3"
           Text="{x:Static prop:Resources.OpenButton}"
           Visibility="{Binding Source={x:Static prop:Settings.Default}, Path=ShowButtonText, 
           Converter={StaticResource BoolToVis}}"></TextBlock>

I 18 also had to include the .Properties namespace 17 in my xaml, like so:

xmlns:prop="clr-namespace:MyProjectNamespace.Properties"

This allowed me to not 16 only use the string resources I had defined 15 for my project for globalization, but I 14 was also able to bind (two way) to my application's 13 Settings. This let me very easily remember the window's 12 position, size, etc. As you can see, use 11 Settings. for settings, and Resources. for 10 resources.

As Steven mentioned, I think the 9 "official" way or the "best" way 8 is to stick x:Uid on everything that you 7 want to globalize, but I didn't and it worked 6 with no problems. I think the x:Uid thing 5 is mostly required if you are using automated 4 tools or breaking the translation task up 3 as you would in a large project. I just 2 did all my own stuff manually in VS, so 1 maybe it was ok.

Ben

Score: 9

Two more additional points that I forgot 3 to mention above in "I was able to do it...":

  1. You don't have to wrap the Properties.Resources object in your own. You can access it directly, as I have done in my example above. I think wrapping the object is a way to get the same result as I discuss in my 2nd point.
  2. By default, the resource files are built with "ResXFileCodeGenerator". This makes them internal when it generates the code file, so xaml can't access it. You have to change this to "PublicResXFileCodeGenerator", which generates public classes. You can change this by clicking the resource file in the solution explorer and editing the "Custom Tool" property.

(sorry, I 2 couldn't edit my above post because I was 1 a temporary member at that time.)

Score: 7

Create a static class that makes the resources 2 available as properties:

public static class Resources
{
   public string Resource
   {
      return Properties.Resources.ResourceManager.GetString("Resource");
   }
}

Then you can bind 1 your TextBox to this:

<TextBlock Text="{Binding Source={x:Static local:Resources}, Path=Resource}" x:Name="upArrowUsersHeader" HorizontalAlignment="Center"
   xmlns:local="clr-namespace:MY_NAMESPACE;assembly=MY_ASSEMBLY">
Score: 7

As Ben said, and I found an another tutorial. The access modifier of Resources.resx should 2 be changed from Internal to Public. I failed many times 1 and after changing the access modifier to Public, it does work.

Score: 4

None of those answers are close to what 4 you want. I'd start by reading about Localization 3 in WPF. You'll find that if you are doing 2 localization with WPF you'll want x:Uid 1 defined on every node in your app.

http://msdn.microsoft.com/en-us/library/ms788718.aspx

Score: 1

I don't know if this can be done directly 6 in XAML but if you write your own wrapper 5 class around ResourceManager and use it 4 instead. Notice that the class inherits 3 from TextBlock:

public class ResourceContentTextBlock : TextBlock
{
    public string ResourceName 
    {
        set
        {
            this.Text = Properties.Resources.ResourceManager.GetString(value);
        }
    }
}

You can then use ResourceContentTextBlock 2 in your XAML anywhere you would otherwise 1 use a TextBlock:

<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:client="clr-namespace:WpfApplication3" 
    >
        <client:ResourceContentTextBlock ResourceName="String1" />
</Window>

More Related questions