rhyous / WPFSharp.Globalizer

Other
26 stars 11 forks source link

HowTo access WPFSharp.Globalizer from code behind/ViewModel file (e.g. for a MessageBox) #18

Open Daimonion1980 opened 2 years ago

Daimonion1980 commented 2 years ago

Hi

What do I have to do when a MessageBox.Show(...) in a code behind file shall use WPFSharp.Globalizer? I did not find any example for this.

Actually i am using a delegate from viewmodel to view to setup a function when the messagebox should appear:

UCCommunicationViewModel? viewmodel = this.DataContext as UCCommunicationViewModel;
if (viewmodel != null)
{ 
    viewmodel.MBErrorInitialize = (title, text) =>
    {
        //title and text contains the keys for globalizer
        return MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Error);
    };
}

How can i evaluate the keys in title and text to print out the localized strings?

rhyous commented 2 years ago

Isn't MesageBox.Show windows forms, not WPF? I think you would need to replace the WinForms version with a WPF message box.

rhyous commented 2 years ago

OK, there is 1.2.1 NuGet package just released. Give a look at tell me if it works well for you.

Daimonion1980 commented 2 years ago

Thanks for your response. I appreciate it.

There is a messagebox in wpf too: https://docs.microsoft.com/en-us/dotnet/api/system.windows.messagebox?redirectedfrom=MSDN&view=windowsdesktop-6.0

You are using it in available Languages ;) https://github.com/rhyous/WPFSharp.Globalizer/blob/32a8d8cd625540c097085793a52f974979c17c2f/src/WPFSharp.Globalizer/AvailableLanguages.cs#L60

I think my question question regarding on how to use globalizer together with message box should be more like 'How to use globalizer directly from a code behind file'

Is it possible for you to give me an example how to use globalizer from code behind?

Also thanks for your last commit. I will take a look into it.

Daimonion1980 commented 2 years ago

As stated in my last comment, i think the question should be more like accessing the globalizer from code behind file.

So, are you able to give me a short example?

Kindly regards

Daimonion1980 commented 1 year ago

Hey,

One Year later and actually I'm in the need to access WPFSharp.Globalizer for the presentation of validation errors in MaterialDesignInXAML Toolkit.

Maybe this could be a new issue, but since my understanding for this problem is, that i need to access the binding interface WPFSharp.Globalizer implements from code level i think a possible solution could be the solution for both problems.

I have a Textbox styled with MaterialDesignInXAML which validates the input via Binding.Validation. The DataContext implements INotifyDataErrorInfo which validates the input and adds error strings to the GetErrors(...) method.

<TextBox Style="{StaticResource MaterialDesignOutlinedTextBox}"
                         Width="250" Padding="10" VerticalAlignment="Center" 
                         Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                         materialDesign:ValidationAssist.UsePopup="True"
                         materialDesign:TextFieldAssist.HasClearButton="False"
                         materialDesign:TextFieldAssist.PrefixText=""
                         materialDesign:TextFieldAssist.SuffixText="{localization:LocalizationBinding LocalizationUnit, FallbackValue=''}" />

These strings, in my case, are strings which are passed to LocalizationBinding interface from WPFSharp.Globalizer. Without MaterialDesignInXAML the Textbox would get an style template Validation.ErrorTemplate and inside this template LocalizationBinding does it's job great.

<Style TargetType="TextBox">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <AdornedElementPlaceholder />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <StackPanel>
                    <Border Padding="{TemplateBinding Padding}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <ScrollViewer x:Name="PART_ContentHost" />
                    </Border>
                    <ItemsControl FontWeight="Light" Margin="0 5 0 0"
                                    ItemsSource="{TemplateBinding Validation.Errors}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Foreground="Red" Text="{localization:LocalizationBinding ErrorContent, FallbackValue='undefined localization return value'}" />
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

But when using MaterialDesignInXAML I'm not able to use LocalizationBinding Interface. So actually I want to use it in ViewModel and pass the evaluated error strings directly to GetErrors List.

specific question:

How am i able to access LocalizationBinding.WhichMethodIHaveToCall(MyValidationString) from code level?