LocExtension with Binding
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:
Sample usage in ComboBox:
Resource dictionary:
Is there and other way of doing this?
Thanks for answers!
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
Sample usage in ComboBox:
Code: Select all
<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>
Code: Select all
<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>
Thanks for answers!
Re: LocExtension with Binding
Additional information: when your MarkupExtension returns this, it does not get triggered again for items inside ItemTemplate
-
sfernandez
Site Admin
- Posts: 3008
- Joined:
Re: LocExtension with Binding
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.
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?
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.
Code: Select all
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding .}" />
</DataTemplate>
Triggered how? The ProvideValue method is only called by the XAML parser when it finds the markup extension assigned to the property.when your MarkupExtension returns this, it does not get triggered again for items inside ItemTemplate
Re: LocExtension with Binding
Hi sfernandez,
How it works in WPF:
Thanks
Here is an example:Triggered how? The ProvideValue method is only called by the XAML parser when it finds the markup extension assigned to the property.
Code: Select all
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);
}
- 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
- On startup it runs once ProvideValue, then it is not run anymore when opening combobox
Reported under: https://www.noesisengine.com/bugs/view.php?id=3101Regarding 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?
Thanks
-
sfernandez
Site Admin
- Posts: 3008
- Joined:
Re: LocExtension with Binding
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?
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?
Re: LocExtension with Binding
Reported under: https://www.noesisengine.com/bugs/view.php?id=3114Regarding 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?
-
sfernandez
Site Admin
- Posts: 3008
- Joined:
Re: LocExtension with Binding
Thanks for both tickets.
Who is online
Users browsing this forum: Bing [Bot], Semrush [Bot] and 8 guests