Setter & Binding usage with XAMLMarkupExtension 2.0
Opened this issue · 9 comments
update Docu:
<TextBlock Name="MyLabel3" FontSize="20" HorizontalAlignment="Center">
<TextBlock.Text>
<MultiBinding Converter="{lex:StringFormatConverter}" >
<Binding Source="{lex:Loc HelloWorldWPF:Ressourcen:MyLabel2}" />
<Binding Path="Hours" FallbackValue=""/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<CheckBox Name="CheckBox" Content="Change text" />
<TextBlock FontSize="20">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding Source={lex:Loc en}}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=CheckBox, Path=IsChecked}" Value="True">
<Setter Property="Text" Value="{Binding Source={lex:Loc de}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Hi, I just saw this issue and I was wondering: should the first case still work if the LocExtension
uses a binding? Something like:
<Binding Source="{lex:Loc {Binding MyProperty}}" />
I tried it but it seems to give me an empty string instead. My use case is also slightly different because I want the binding for one of the arguments, not the string format, but I guess they are probably handled in the same way.
Otherwise, is there a recommended way of achieving that? I could only find a workaround by using a dummy, collapsed framework element (it could be an FELoc
), setting the binding for one of the properties there, then define the binding as normally:
<Binding ElementName="TheDummyElement" Path="ThePropertyOfTheDummyElementWithTheBinding" />
Other than this, great library so far!
Hi, good question. I will test this, but the issue here might be that Binding is a MarkupExtension and not a DependencyObject. It can be that the necessary changes are still not in the pre / release.
Normally this Binding inside a MarkupExtension is not possible, but karnah and I found together
a cool workaround to make this possible, but this is not working in all cases.
Following the example above, I'm trying to change the value of a Setter:
<DataTrigger Binding="{Binding XPath=configurations/PatientType}" Value="PMI"> <Setter x:Name="LoginPageTitlePMI" Property="Text" Value="{Binding Source={lex:Loc en}}"/> </DataTrigger>
This displays "Key: en" and not the correct string. Also, I would like to be able to set any language (as in Text="{lex:Loc}")
@konne Could you provide any help?
@NiklasMoller, hi!
It's a assumption. but looks like your case. There is a problem with using Loc
inside Binding
. Extension cannot access to LocalizationProvider
which connected with UI element. So you need to set provider using LocalizeDictionary.Instance.DefaultProvider
.
@Karnah Thanks! Could you provide an example in code of what you describe?
@NiklasMoller, hmm, I've checked your case again. It should be enough to set this lines in main window.
WPFLocalizeExtension/tests/HelloWorldWPF/MainWindow.xaml
Lines 13 to 14 in f704cdc
I put this lines to MainWindow.xaml and it works fine:
<TextBlock FontSize="20" HorizontalAlignment="Center" Foreground="Aqua">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{lex:Loc en}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Source={x:Static lex:LocalizeDictionary.Instance}, Path=Culture.TwoLetterISOLanguageName}"
Value="de">
<Setter Property="Text" Value="{Binding Source={lex:Loc de}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
If you have same lines, could you create a sample project?
@Karnah Thanks! I tried it without any luck.
I have setup an example repo here with my use case described: https://github.com/NiklasMoller/WPFLocalizationExtensionsWithSetters
Could you please help me and others struggling with this? Thanks :)
@NiklasMoller, sorry, I didn't understand your problem first time.
So, problem is that autofinding keys in Binding
is not working. When extension is used inside 'Binding' and Setter
, it can't get information about target object. I know only one solution which can help you: specify key
<Style x:Key="SelectUserStyle" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding XPath=configurations/PatientType}" Value="PLP">
<Setter x:Name="LoginPageTitlePLP" Property="Text" Value="{Binding Source={lex:Loc LoginPageTitlePLP, ForceCulture='sv-SE'}}"/>
</DataTrigger>
<DataTrigger Binding="{Binding XPath=configurations/PatientType}" Value="SCI">
<Setter x:Name="LoginPageTitleSCI" Property="Text" Value="{Binding Source={lex:Loc LoginPageTitleSCI, ForceCulture='sv-SE'}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
Also you said about to be able to set any language
. You meen property ForceCulture
ot something else?
@Karnah Brilliant! Specifying the key works fine 👍
With "to be able to set any language" I meant that the text should adapt depending on which CultureInfo is being specified. Works fine just specifying the key.
Many thanks for your help with this 🥇