2

Is there any good tutorial on how to use ResourceManager in WPF binding ? I know how to use it with the GetString function to retrieve a specific resource, but I also would like to use in in binding.

something like

<TextBlock Text="{Binding Resource.EnterName}"/>

thanks

H.B.
  • 166,899
  • 29
  • 327
  • 400
David Brunelle
  • 6,528
  • 11
  • 64
  • 104

1 Answers1

2

A binding would not do much except getting the value once as the resources do not provide any notifications as far as i know. You can get the values using x:Static, e.g.:

<TextBlock xmlns:prop="clr-namespace:AppAssemblyName.Properties"
           Text="{x:Static prop:Resources.ResourceName}"/>

(The resource's accessors need to be set to public)

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • 1
    Tried it but I can't make it work... I don't seem to be able to get the 'prop' namespace as it doesn't let me do it – David Brunelle Sep 24 '11 at 15:39
  • @DavidBrunelle: Did you set the access modifiers to public in the designer toolbar? – H.B. Sep 24 '11 at 15:41
  • Nevermind I found what you are talking about (I wasn't the one who did the resource). Still, I can't get the 'Properties' part of the namespace to work – David Brunelle Sep 24 '11 at 15:54
  • Just got it working... It was Resources, not Properties that I used but thanks. It's working now – David Brunelle Sep 24 '11 at 15:56
  • I was just wondering. Do you think this could work with different cultures ? The reason we want this is to be able to translate the label for some user. The culture would be set before anything show. – David Brunelle Sep 27 '11 at 12:59
  • @DavidBrunelle: It should work, as long as the properties are implemented as expected, getting the string using `ResourceManager.GetString(name, culture)`. – H.B. Sep 27 '11 at 13:09
  • 1
    @H-B I'm at a lost here. I don't want to use a function GetString with a culture argument since it will be directly in the XAML... Does that mean it can't be done that way? I guess the best thing would be to test it. – David Brunelle Sep 27 '11 at 13:41
  • @H-B Just tried it. It works like you mentionned at first. I have to change the resource culture before doing the initialiseComponents for now, so I guess there is a way to force the 'refresh' to prevent doing this. – David Brunelle Sep 27 '11 at 14:00
  • 1
    @DavidBrunelle: If you have `.resx` resources the properties are automatically implemented using `GetString`, e.g. `Resource.EnterName` should look like this `public static string EnterName { get { return ResourceManager.GetString("EnterName", resourceCulture); } }` in the `Resource.Designer.cs` which is created automatically. – H.B. Sep 27 '11 at 14:02