I've got an application where I need to show a price, for that, I have the following code:
<Label Content="{Binding Prijs}" ContentStringFormat="C"></Label>
However, that gives a stringformat like: $10.00, but i want to show the euro-sign (€) instead of the dollar-sign ($). How do I do that?
You need to make sure that the Language of the control is set correctly.
Tim Heuer has a blog post entitled "StringFormat and CurrentCulture in Silverlight" about this for Silverlight so I expect the same problem occurs in WPF.
The solution for Silverlight is to add the following line to the view constructor:
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
Now for WPF you might just need to make sure that the CurrentThread.CurrentCulture is set correctly, if not try adding this line too.
Related
I tried to localize my Windows Universal App with the Multilingual app toolkit.
Because you can't bind strings directly from a .resw file in XAML, I used the ReswFileCodeGenerator tool. It works great in code behind, but in XAML I can't get this right.
In Windows Phone 8.0 I could use:
Text="{Binding Path=LocalizedResources.StringName, Source={StaticResource Strings}}"
Is there a similar way in Windows (Phone) 8.1 with the ReswFileGenerator tool?
I would still suggest you do it as Depechie already suggested: use x:Uid attribute.
If you need to get the localized string from code, just replace . in resource name with /. So, in XAML you'll write:
<TextBlock x:Uid="appString" />
In code you'll use:
var resourceLoader = new ResourceLoader();
var localizedText = resourceLoader.GetString("appString/Text");
When doing resource translations in wp8.1 with .resw files, you need to use the x:Uid attribute on your xaml control!
Like <TextBlock x:Uid="FieldKey" />
Details are mentioned here...
I've coded something to help me with this. It may not be perfect, but works great for me. Here's a link to the helper. You build it and put the .exe file in some easy-to-reach folder.
In the project with the resources, you set the pre-build action to something like this (you only need to change the "path\to\ResourcesHelper.exe" part):
call "path\to\ResourcesHelper.exe" "$(TargetName)" "$(ProjectDir)\" "$(RootNameSpace)" "universal"
Also, the main resources must be in Resources/en-US folder of your project (you can change it in the code, though).
This way, when you build the project, it will generate a file called LocalizedStrings.cs which is something like the file generated for .resx files. It contains some additional properties called LC (for lower case), UC (for upper case) and UCF (for upper case first) which return the strings in that casing. I hope you'll find it useful. :)
Note: The tool wasn't meant for other people, so I really just coded what I needed, so it may not work flawlessly.
Here is my context :
We are using WPF to create an new Windows user interface for our product. As we are cross-platform, all information as Label.Content or Button.Content are known in an other part of the application (written in C), and not defined in XAML.
Here is the problem :
We want to handle Strings that we put in a WPF component's content.
I see that we have some attributes as
Label.Content.FontFamily or Label.Content.Size, but graphic attributes are not necessary the same for all the String.
For exemple :
This is my label's content : "Hello guys, thank you to help me". Is it possible to
Underline "thank you"
Change all uppercases in Red color
Change the size of these uppercases
Actually, we are using WinFroms to do that, but it is time to renovate the GUI, because Winforms are just ugly now.
Thanks a lot !
The TextBlock's Content property (Text) expects a collection of Inline elements (InlineUICollection), you have the following available:
Inline
InlineUICollection
LineBreak
Run (defines Text property)
Span (defines Inlines property)
Bold
Italic
Underline
(Inlines is the Content property for span and it's an InlineUICollection, meaning that you can add Run and LineBreaks and other Spans/Bold/Italic inside Span.)
Using this it is possible to "Hello guys, thank you for helping me":
<TextBlock><Run Text="H" FontSize="20" Foreground="Red"/><Span>ello guys, <Underline>thank you</Underline> for helping me</Span></TextBlock>
The H will be red and bigger, and thank you will be underlined.
This is all possible programmatically as well, for example, "hello world" using a run in a textblock:
TextBlock t = new TextBlock();
t.Inlines.Add(new Run{Text="Hello World"});
I have a small C# project that has an ApplicationBar. But I have a small problem: I want 8 icons on the bar, and the ApplicationBar only supports 4. I came up with a solution (in C#): add a small CheckBox to ask if the user wants to use the first or second set of tools.But I'm still not able to change the icons on the ApplicationBar. I tried removing the old ones, first with ApplicationBar.MenuItems.Remove(Button1); and then with ApplicationBar.Buttons.Remove(Button1);
but neither worked. I tried changing the .IconUri property of the button, but that gave me a NullReferenceException.
I don't understand what you mean by changing it from "C#, not Silverlight". C# is a programming language and Silverlight is a framework. Nevertheless, the link you posted to explains exactly how you do it. The ApplicationBar is not a Silverlight control, it's part of the native OS. You can use the code in the link or do something like this:
firstAppBarButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
firstAppBarButton.Text = "New Text";
firstAppBarButton.IconUri = new Uri("/appbarIcon.png",UriKind.Relative);
You need to get the ApplicationBarIconButton via the index (0 for first one, 1 for second etc..) instead of by name.
You can't refer to the application buttons by name. Try:
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Remove
I would also suggest that you do not present two groups of 4 icons to the user. The limit is 4 for a reason. Any more than that requires a UI re-think. Perhaps divide the functionality over a few pages?
The syntax above gave me a compile error. With some additional research, I got this to work for me:
ApplicationBar.Buttons.Remove((ApplicationBarIconButton) ApplicationBar.Buttons[0]);
I have a small C# project that has an ApplicationBar. But I have a small problem: I want 8 icons on the bar, and the ApplicationBar only supports 4. I came up with a solution (in C#): add a small CheckBox to ask if the user wants to use the first or second set of tools.But I'm still not able to change the icons on the ApplicationBar. I tried removing the old ones, first with ApplicationBar.MenuItems.Remove(Button1); and then with ApplicationBar.Buttons.Remove(Button1);
but neither worked. I tried changing the .IconUri property of the button, but that gave me a NullReferenceException.
I don't understand what you mean by changing it from "C#, not Silverlight". C# is a programming language and Silverlight is a framework. Nevertheless, the link you posted to explains exactly how you do it. The ApplicationBar is not a Silverlight control, it's part of the native OS. You can use the code in the link or do something like this:
firstAppBarButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
firstAppBarButton.Text = "New Text";
firstAppBarButton.IconUri = new Uri("/appbarIcon.png",UriKind.Relative);
You need to get the ApplicationBarIconButton via the index (0 for first one, 1 for second etc..) instead of by name.
You can't refer to the application buttons by name. Try:
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Remove
I would also suggest that you do not present two groups of 4 icons to the user. The limit is 4 for a reason. Any more than that requires a UI re-think. Perhaps divide the functionality over a few pages?
The syntax above gave me a compile error. With some additional research, I got this to work for me:
ApplicationBar.Buttons.Remove((ApplicationBarIconButton) ApplicationBar.Buttons[0]);
I am working on a WPF application. I set up a multilanguage with xml files and I use static resource binding in front code to set the corrisponding text. The issue I have is with doing the same thing in the codebehind.
Here you can see how I use it in front code:
<XmlDataProvider x:Key="Lang" Source="/lng/english.xml" XPath="WpfApplication"/>
<Label HorizontalAlignment="Center" Margin="0,10,0,5" Foreground="White" FontWeight="Bold" Content="{Binding Source={StaticResource Lang}, XPath=MenuTextClimate/#Header}"></Label>
I am trying to do the same in codebehind like this:
String selLangFullPath = WpfLibrary.LanguageOptions.getSelLangFullPath();
XmlDataProvider xmlData = (XmlDataProvider)(this.FindResource("Lang"));
xmlData.Source = new Uri(selLangFullPath, UriKind.Relative);
xmlData.XPath = "MenuTextClimate/#Header";
Binding NewBinding = new Binding();
NewBinding.Source = xmlData;
NewBinding.Mode = BindingMode.OneWay;
NewBinding.XPath = "MenuTextClimate";
lblTitle.SetBinding(Label.ContentProperty, NewBinding);
but for some reason it doesent seem to work. Can any one tell me where I went wrong?
The codebehind you've shown doesn't actually do the same thing. It's different in three ways:
You're changing the Source property of the XmlDataProvider
You're providing a different XPath to the XmlDataProvider (MenuTextClimate/#Header instead of WpfApplication).
You're also providing a different XPath in the binding expression.
The problem could simply be that any or all of those things is wrong. (The XPath ones look particularly suspicious, because they look like they presume a completely different XML document structure. Although since you're also providing a different XML document, maybe that's fine. It's impossible to tell from the information provided so far.) So the first thing I'd do is try making your C# do exactly the same as your Xaml - same URI and same XPaths. If that works, it should be easier to see which of the three things that's different is causing the problem.
Alternatively, enable WPF debug output. If you're on .NET 3.5 sp1 or earlier, this is usually on by default for Error level logging of data binding messages. (Data binding errors appear in the Output window.) As of .NET 4.0, Microsoft turned it down so you won't see it unless you ask for it. You turn it on with the 'options' dialog in Visual Studio - it's under Debugging -> Output Window. Ensure that Data Binding is set to show errors. Or for more detail, crank it all the way up and then enable full logging by adding this:
PresentationTraceSources.SetTraceLevel(NewBinding, PresentationTraceLevel.High);
That should show you full gory details of what data binding is attempting to do with your binding, and that's often a pretty good way to find out why things aren't working.