How to fetch string from resource to assign in WPF Resource section in xaml How to fetch string from resource to assign in WPF Resource section in xaml wpf wpf

How to fetch string from resource to assign in WPF Resource section in xaml


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 also had to include the .Properties namespace in my xaml, like so:

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

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

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

Ben


Two more additional points that I forgot 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 couldn't edit my above post because I was a temporary member at that time.)


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