robert-j-engdahl / ngettext-wpf

Proper internationalization support for WPF (via NGettext)
GNU Lesser General Public License v3.0
10 stars 12 forks source link

Gettext xaml MarkupExtension does not support styles #10

Open Kirlu opened 6 years ago

Kirlu commented 6 years ago

When adding a translation to a style where the translation changes based on fx. DataTriggers this throws an Exception.

System.Windows.Markup.XamlParseException: ''Provide value on 'NGettext.Wpf.GettextExtension' threw an exception.' Line number '61' and line position '48'.' Inner Exception: InvalidCastException: Unable to cast object of type 'System.Windows.Setter' to type 'System.Windows.DependencyObject'.

<Style TargetType={x:Type Button>
    <Setter Property="Content" Value="{wpf:Gettext Close}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding CanOpen}" Value="True">
            <Setter Property="Content" Value="{wpf:Gettext Open}" />
        </DataTrigger>
    </Style.Triggers>
</Style>
robert-j-engdahl commented 6 years ago

I could kind of remove the bug, but changing language would then not be supported. But I agree this should not crash. This library is not supposed to get in your way.

In the concrete example, one might want to use two buttons instead though, and the collapse one of them depending on the state.

This could also be solved from the view-model quite easily.

    public string ButtonText => CanOpen ? _("Open") : _("Close");

    PropertyChanged += (s, e) => { 
        if (e.PropertyName == "CanOpen")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ButtonText"); 
        }
    };