Otter
Topic Author
Posts: 18
Joined: 23 Sep 2023, 12:04

LocExtension with Binding

19 Feb 2024, 18:30

Hello,

I'm trying to use LocExtension provided by Noesis and I've encountered that I cannot dynamically bind properties to this extension, something like this: Text="{noesis:Loc {Binding WorldsText}}"

I have tried to extend this class with Binding property, but then I've encountered a few issues:
  • Calling BindingOperations.SetBinding(...);, then targetObject.GetValue(targetProperty) will always return null, which then I tried to fix with a converter
  • Above problem was fixed with a workaround to use converter inside binding, but then DependencyObject for items inside combobox item template always returns the same reference between different items, which means changing one property value via TargetObject.SetValue will change all of them
It is also worth mentioning that ItemSource for this ComboBox is an Enum and that the key for localization is the Enum value itself.

Sample usage in ComboBox:
<ComboBox 
          ItemsSource="{Binding Source={providers:EnumItemsSourceProvider EnumType={x:Type enums:DummyEnum}}}"
          SelectedItem="{Binding SelectedDummyEnum}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock HorizontalAlignment="Center"
                       Text="{noesis:Loc Binding={Binding .}}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
Resource dictionary:
    <sys:String x:Key="FirstEnum">Translate First Enum</sys:String>
    <sys:String x:Key="SecondEnum">Translate Second Enum</sys:String>
    <sys:String x:Key="ThirdEnum">Translate Third Enum</sys:String>
Is there and other way of doing this?

Thanks for answers!
 
Otter
Topic Author
Posts: 18
Joined: 23 Sep 2023, 12:04

Re: LocExtension with Binding

20 Feb 2024, 11:52

Additional information: when your MarkupExtension returns this, it does not get triggered again for items inside ItemTemplate
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: LocExtension with Binding

20 Feb 2024, 12:26

Hi Otter,

The LocExtension was designed to use static keys, because our understanding was that any dynamic data will be provided by the MVVM itself. So instead of using the LocExtension you would directly use a binding on the target property, and the binding will provide the translated data.
<DataTemplate>
  <TextBlock HorizontalAlignment="Center" Text="{Binding .}" />
</DataTemplate>
Regarding the problems you found modifying the LocExtension I think our XAML parser does not correctly support properties of type Binding that are not DependencyProperty. Could you please report this in our bugtracker?
when your MarkupExtension returns this, it does not get triggered again for items inside ItemTemplate
Triggered how? The ProvideValue method is only called by the XAML parser when it finds the markup extension assigned to the property.
 
Otter
Topic Author
Posts: 18
Joined: 23 Sep 2023, 12:04

Re: LocExtension with Binding

20 Feb 2024, 13:06

Hi sfernandez,
Triggered how? The ProvideValue method is only called by the XAML parser when it finds the markup extension assigned to the property.
Here is an example:
public override object ProvideValue(IServiceProvider serviceProvider)
{
    var valueTarget = serviceProvider as IProvideValueTarget;
    if (valueTarget == null)
    {
        return null;
    }

    var targetObject = valueTarget.TargetObject as DependencyObject;
    
    // If inside template, target object will be null and WPF will call ProvideValue again when template is applied
    if (targetObject == null)
    {
        return this;
    }
    
    DependencyProperty targetProperty = (DependencyProperty)valueTarget.TargetProperty;
    return targetObject.ProvideValue(targetProperty);
}
How it works in WPF:
  • On startup when ComboBox is not open it runs ProvideValue once
  • Then when you open combobox it runs ProvideValue for each element inside of it
How it works in Noesis:
  • On startup it runs once ProvideValue, then it is not run anymore when opening combobox
Regarding the problems you found modifying the LocExtension I think our XAML parser does not correctly support properties of type Binding that are not DependencyProperty. Could you please report this in our bugtracker?
Reported under: https://www.noesisengine.com/bugs/view.php?id=3101

Thanks
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: LocExtension with Binding

22 Feb 2024, 10:53

Thanks for the report!

Regarding the issue with ProvideValue not being called when templates are applied I understand now what is happening. This is a limitation of our current implementation of templates. Could you please report this as a separate issue?
 
Otter
Topic Author
Posts: 18
Joined: 23 Sep 2023, 12:04

Re: LocExtension with Binding

22 Feb 2024, 14:53

Regarding the issue with ProvideValue not being called when templates are applied I understand now what is happening. This is a limitation of our current implementation of templates. Could you please report this as a separate issue?
Reported under: https://www.noesisengine.com/bugs/view.php?id=3114
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: LocExtension with Binding

26 Feb 2024, 16:56

Thanks for both tickets.

Who is online

Users browsing this forum: No registered users and 5 guests