c# - localization - changing language in wpf app - c#

I am making some wpf application and I need option for changing languages. I have folder named Resorces in my solution, where I store all my resx file - actually language.resx and language.en-EN.resx. My XAML looks like:
<Window x:Class="CoinCatalogue.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CoinCatalogue"
xmlns:resx="clr-namespace:CoinCatalogue.Resources"
mc:Ignorable="d"
Title="{x:Static resx:language.WindowName}" Height="480" Width="640">
<Grid>
<Menu x:Name="menu" HorizontalAlignment="Left" Height="auto" VerticalAlignment="Top" Width="517">
<MenuItem x:Name="File" Header="{x:Static resx:language.File}">
<MenuItem x:Name="Open" Header="{x:Static resx:language.Open}"/>
</MenuItem>
</Menu>
</Grid>
Everything is ok - I have access to string in app. And in my main class I am trying to change culture with line:
language.Culture = new System.Globalization.CultureInfo("en-EN");
But nothings happen to my app. This line is before InitializeComponent();.
What am I doing wrong? I am using Visual Studio 2015. /is it a good idea to change app language in this way?

I think how you are changing the culture is the issue.
I just put a quick sample together where I had the following resx files:
Resources.en-US.resx
Resources.fr-FR.resx
Resources.resx
If I run the app without attempting to change the culture, I get the strings from the Resources.en-US.resx file as that is my default. If I add this code before InitializeComponent my strings come from the Resources.fr-FR.resx file:
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("fr-FR");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("fr-FR");

Related

Cannot select objects in XAML Designer when using dragablz and MahApps

I am developing an application with the use of dragablz, MahApps and MaterialDesign. However, I have broken down the problem to the dragablz and MahApps extension.
I create two tab items (extract from demo code):
<controls:MetroWindow x:Class="MahMaterialDragablzMashUp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz"
xmlns:mahMaterialDragablzMashUp="clr-namespace:MahMaterialDragablzMashUp"
xmlns:dockablz="clr-namespace:Dragablz.Dockablz;assembly=Dragablz"
xmlns:wpf="http://materialdesigninxaml.net/winfx/xaml/themes"
WindowTransitionsEnabled="False"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
Background="{DynamicResource MaterialDesignPaper}"
GlowBrush="{DynamicResource AccentColorBrush}"
FontFamily="{StaticResource DefaultFont}"
Title="Material-MahApps-Dragablz Mash Up" Height="640" Width="800">
<dockablz:Layout>
<dragablz:TabablzControl BorderThickness="0"
Margin="0,-1,0,1">
<dragablz:TabablzControl.InterTabController>
<dragablz:InterTabController />
</dragablz:TabablzControl.InterTabController>
<TabItem Header="DIALOGS">
<mahMaterialDragablzMashUp:Mah Margin="16" />
</TabItem>
<TabItem Header="MAHAPPS">
<mahMaterialDragablzMashUp:Mah Margin="16" />
</TabItem>
</dragablz:TabablzControl>
</dockablz:Layout>
</controls:MetroWindow>
The code works fine, but when I try to switch to another tab in the XAML designer by clicking on it, the whole section is selected but not any single object, so I cannot click on another tab:
The view in the designer changes when I click into the corresponding code lines in the XAML editor, but not when I click on the object in the designer. This makes simple design steps (like moving buttons around) very difficult as I cannot click and drag them.
Up to now, I have tried the following:
using Visual Studio 2013 Professional
using Visual Studio 2015 Community
Build the project
restart VS and the computer
I found the following question and the provided solution by 'JFTxJ' (that empty Grid elements are causing the problem) is not working for me, as I do not have any Grid items.
Same for the answer from 'Moon Waxing' (to add the attribute Visibility="Collapsed" to the tabiItem) is not working for me.
Other StackOverflow question: Cannot click or select Control XAML design
Is this a common problem or is there any solution to it?

VS Designer not working when adding a user control from another project

I am creating a universal app in Visual Studio 2015. My universal app has a reference to a universal library called UIComponents.
In UIComponents I created a user control:
namespace MyProj.UIComponents {
public sealed partial class MyControl : UserControl
{
public MyControl()
{
this.InitializeComponent();
}
}
}
With the following xaml:
<UserControl
x:Class="MyProj.UIComponents.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProj.UIComponents"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<Rectangle Fill="White" HorizontalAlignment="Left" Height="280" Stroke="Black" VerticalAlignment="Top" Width="380" Margin="10,10,0,0"/>
<TextBox x:Name="textBox" Margin="20,20,20,20" TextWrapping="Wrap" Text="TextBox"/>
</Grid>
</UserControl>
Inside my app project, which references UIComponents, I do this:
<Page
x:Class="MyProj.App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProj.App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="using:MyProj.UIComponents"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ui:MyControl></ui:MyControl>
</Grid>
</Page>
But when I try to get the designer display the page I get:
The error list shows this:
The name "MyControl" does not exist in the namespace
"using:MyProj.UIComponents".
Funny thing is that the whole solution builds just fine, but the designer is not collaborating.
Attempt using clr-namespace
There are similar questions about this in WPF, so not strictly universal apps, and they are marked as solved on answers where the solution was to use:
xmlns:ui="clr-namespace:MyProj.UIComponents"
Bu that does not work:
Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace
'MyProj.UIComponents' that could not be found.
The error list shows this:
The name "MyControl" does not exist in the namespace "using:MyProj.UIComponents".
In my experience, this could probably be caused by
The UIComponents Library didn't got built.
After building the Library project, VS will add the following codes into YourProject.proj file, which will be detected by VS designer:
<ItemGroup>//The following lines will be added.
<Page Include="MyControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
So, please try building your library project and rereference it in your main project.After reloading, designer should load the contents correctly.
Notes: The architecture(x64,x86) when building your library should be identical to current architecture. (e.g. when you build your library with x86. Designer can't load correctly, when your current architure is x64).
The namespace is wrong, which seems not the main cause here.

How can I display the current day of the week in XAML using the current regional settings?

I have the following XAML:
<Window x:Class="String_Format.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:s="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="250">
<StackPanel Margin="10">
<TextBlock Name="TextBlock" Text="{Binding Source={x:Static s:DateTime.Now}, StringFormat=Date: {0:dddd, MMMM dd}}"/>
</StackPanel>
</Window>
In the XAML designer in Visual Studio 2015 as well as in the running application, this displays as: "Date: Thursday, January 28". As I am on a German Windows 7 with regional settings set to "German (Germany)", I had actually expected: "Date: Donnerstag, Januar 28".
So I went and added the namespace
xmlns:c="clr-namespace:System.Globalization;assembly=mscorlib"
and altered my TextBlock to:
<TextBlock Name="TextBlock" Text="{Binding Source={x:Static s:DateTime.Now}, ConverterCulture={x:Static c:CultureInfo.CurrentCulture}, StringFormat=Date: {0:dddd, MMMM dd}}"/>
This indeed results in my desired behavior at runtime, but in the XAML designer, it says "Invalid Markup", and there's no preview of my window displayed an more.
This has been asked before, but I wanted to do it inside XAML, without using code behind, if possible.
So what's wrong with my XAML? I'd like to understand.
This indeed results in my desired behavior at runtime, but in the XAML designer, it says "Invalid Markup", and there's no preview of my window displayed an more.
This is because the added code: ConverterCulture={x:Static c:CultureInfo.CurrentCulture} cannot be resolved at design time. This results in the invalid markup and lack of display. It is resolved at runtime, which is why your date displays in the correct language/culture.
You can only solve this by moving this into code where you can check whether it's runtime or design time.
Checkout if setting the Language on the Window helps, I do this in the code behind of a window / usercontrol etc:
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

Customise WinRT XAML Toolkit - Calendar Control

I wrote a Windows Phone 8.1 (WINRT) App. I need to show Calendar in the page. So, I added WinRT XAML Toolkit - Calendar Control from nuget.
PM> Install-Package WinRTXamlToolkit.Controls.Calendar
<Page
x:Class="DrFit.Pages.ActivityTimeTablePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DrFit.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:WinRT="using:WinRTXamlToolkit.Controls"
Background="Black">
<Grid x:Name="LayoutRoot">
<WinRT:Calendar Height="500">
</WinRT:Calendar>
</Grid>
</Page>
How to Customise this Calendar control, example FontWeight,Foreground,Background?
If the properties aren't exposed by the control or template-bound to template part properties - you'll probably need to change the template. You can find the default template here and templates for calendar parts are in the same folder:
WinRTXamlToolkit.Controls.Calendar/WinRTXamlToolkit.Controls.Calendar.Shared/Controls/Calendar
You probably have figured out the answer to this but I was facing a similar problem and figured out a solution so I thought I'd post it here for anyone else who faces a similar conundrum. If the calendar control is going out of the page, you can wrap it inside a Viewbox so that it fits the screen. This is how I managed to do it.
<Viewbox Stretch="Uniform">
<Grid Height="600" Width="600">
<toolkit:Calendar HorizontalAlignment="Center" VerticalAlignment="Center" Tapped="Calendar_Tapped"/>
</Grid>
</Viewbox>

Command Binding with WPF

Im a complete Noob to this so im having a really hard time wrapping my head around how this works.
Basically I have a Main Page that im using, and within the XAML i have created a menu
What I have is a Document (DummyDoc) that contains a TextBox within it that i am trying to send the find command to.
Ive tried this every which way and googled it but i just cant seem to get it to work for me and could use some help with a push in the right direction
Main form
<Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:DMC_Robot_Editor"
xmlns:local="clr-namespace:DMC_Robot_Editor.GUI"
<Menu>
<MenuItem Header="_Edit">
<MenuItem Header="_Cut"/>
</MenuItem>
<MenuItem/>
<Grid>
<local:DummyDoc x:Name="_Editor"/>
</Grid>
</Window>
That is the main form that i am using. then i have my second document "DummyDoc"
<ad:DocumentContent x:Name="document" x:Class="DMC_Robot_Editor.Controls.DummyDoc"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ad="clr-namespace:AvalonDock;assembly=AvalonDock"
xmlns:local="clr-namespace:DMC_Robot_Editor.Controls"
xmlns:ed="schemas.microsoft.com/expression/2010/drawing"
Title="Window1" Height="300" Width="300"
IsVisibleChanged="Is_VisibleChanged" PropertyChanged="document_PropertyChanged">
<Grid>
<Menu >
<MenuItem Header="_File">
<MenuItem Header="was here"/>
</MenuItem>
</Menu>
<local:Editor x:Name="source" IsVisibleChanged="Is_VisibleChanged" TextChanged="TextChanged" UpdateFunctions="raiseupdated" />
<local:Editor x:Name="data" Visibility="Hidden" IsVisibleChanged="Is_VisibleChanged" TextChanged="TextChanged" UpdateFunctions="raiseupdated"/>
</Grid>
</ad:DocumentContent>
DummyDoc is a window that has an Inherited Editor in it.
<avalonedit:TextEditor
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
x:Class="DMC_Robot_Editor.Controls.Editor"
x:Name="editor"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
TextChanged="Text_Changed"
IsVisibleChanged="raiseUpdate"
MouseMove="Mouse_Move"
MouseHover="Mouse_Hover"
MouseHoverStopped="Mouse_Hover_Stopped" KeyUp="editor_KeyUp">
</avalonedit:TextEditor>
My Ultimate Question is how do i use WPF Binding to make the "Cut" Action from the main form initiate the cut() method of the textbox?
I wrote textbox in it because in code behind, im doing the following
partial class DummyDoc:DocumentContent
{
public Editor TextBox{get;set;}
private void Is_VisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender is Editor)
this.TextBox = sender as Editor;
if ((VisibilityChanged != null) && (TextBox != null))
raiseupdated(TextBox, new FunctionEventArgs(this.TextBox.Text));
}
}
ElementName looks up elements by looking for an element which is using the string identifier you specify.
Did you put x:Name="local:TextBox" on your TextBox tag?
I think you've got your wires crossed by using "local:TextBox".
For starters...that is the syntax used to refer to an element type within a namespace .... it means "the type TextBox in the local namespace".....it's not valid (or rather doesn't mean the same) in the context you are using....you should just assign an "identifier" string.
So....
CommandTarget="{Binding ElementName=textboxFind}"
...
<TextBox x:Name="textboxFind" ..... />
would be more appropriate.
UPDATE (in light of question being clarified):
You should specify a "Command" in your menu item which will get raised when you choose that menu item.
Then if the TextEditor has the focus (...and thus is the command target...)....then it should see the Cut command.
I would expect the Avalon Editor to be able to handle the well know "ApplicationCommands" i.e. Cut, Copy, Paste, etc.
<MenuItem Header="_Cut" Command="ApplicationCommands.Cut">

Categories