0

i'm currently programming an uwp app and need some help with xaml binding.

Let's say i have 3 Textblocks (textBlock1, textBlock2, textBlock3) and in the code-behind-file 3 string-properties (Text1, Text2, Text3). The Text-properties from the control are binded to the string-properties:

<TextBlock x:Name="textBlock1" Text="{x:Bind Text1, Mode=OneWay}"/>
<TextBlock x:Name="textBlock2" Text="{x:Bind Text2, Mode=OneWay}"/>
<TextBlock x:Name="textBlock3" Text="{x:Bind Text3, Mode=OneWay}"/>

The string-properties are computed properties and their value changes according to the current culture.

public string Text1 => _resource.GetStringResource("text1");
public string Text2 => _resource.GetStringResource("text2");
public string Text3 => _resource.GetStringResource("text3");

If i receive the event, that the culture was changed, i would like to update the ui.

I did find 3 solutions to solve this problem, but i don't like them, thus asking for advice.

Solution 1:

I can call OnPropertyChanged for every string-property. I don't like this because I must do it for every property.

Solution 2:

My second solution would be to use/misuse a ValueConverter like this.

<TextBlock Text="{x:Bind _resource, Converter={StaticResource ResourceToStringConverter}, ConverterParameter=Text1}"/>

and in the converter to call and return _resource.GetStringResource("converterParameter").

I don't really know why i don't like it, but it feels like im misusing the Converter.

Solution 3: Call this.Binding.Update(). I prefer solution 1 over this.

Thanks in advance.

lampenlampen
  • 947
  • 6
  • 21
  • The wording of the question technically is "primarily opinion based", and arguably "needs focus", and ordinarily the question would be closed due to that. However, you say the only reason you don't like the first option is that you have to add a call for each property, when in fact that's not correct. If you raise the `PropertyChanged` event with the property name set to `null`, then WPF will update _all_ bindings from their source for that object. See duplicate. – Peter Duniho Feb 18 '21 at 01:17
  • @PeterDuniho I should have clarified that. I don't like it because all Bindings get updated, not just the ones, which are localized. But i managed to solve it, by putting all strings in another class `PageStringResources` and bind to it `Resources.Text1` and now i can call `OnPropertyChanged("Resources")`. – lampenlampen Feb 18 '21 at 16:01

0 Answers0