Globalization / multilanguage in WPF vb.net using Ressourcedictionaries .xaml - vb.net

What is the best practice to set up a Multilingual App?
I already have a working project with Ressource-dictionaries. But it looks different to this Topic:
MSDN globalization-and-localization-overview.
This is my approach
Application.Xaml:
<Application.Resources >
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Ressources/Lang_EN.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Project-Explorer:
Explorer
Inside a File Lang_EN.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<!--Public Enum SpindleType
Milling_Spindle = 0
Main_Spindle = 1
Sub_Spindle = 2
Grinding_Spindle = 3
End Enum-->
<system:String x:Key="Timer">Timer</system:String>
<system:String x:Key="Drehzahl">Speed</system:String>
<system:String x:Key="APIStatus">API Status</system:String>
<system:String x:Key="MachineType">Machine type</system:String>
<system:String x:Key="MachineSN">Machine S/N</system:String>
<system:String x:Key="SpindleSN">Spindle S/N</system:String>
In my MainWindow.xaml.vb i have following sub that changes the UI Language:
Private Sub Button_NL_Click(sender As Object, e As RoutedEventArgs) Handles Button_NL.Click
NewDictSource("\Ressources\Lang_NL.xaml")
End Sub
The Mainwindow.xaml looks like that:
<TextBlock
x:Name="lbl_Timer_block3"
MinWidth="120"
VerticalAlignment="Center"
Text="{DynamicResource Timer}" Margin="0,0,10,0" />
Here in the MainWindow.xaml i use the Dynamic Ressource dictionary i declared in Application.xaml before.
Do you have a nicer and more easy way to do this?

Related

silverlight expression in binding

I am styling a usercontrol of mine.
I have some "constants" regarding the size of the object, and in my template object I have something like
<UserControl ...>
<UserControl.Resources>
<sys:Double x:Key="width">10</sys:Double>
<sys:Double x:Key="margin">30</sys:Double>
</UserControl.Resources>
...
<ControlTemplate ...>
<Grid x:Name="width_plus_margin">
...
If I wanted the width of the "witdh_plus_margin" to be "width" value I just added something like
Width="{StaticResource width}"
but what I really need is to set something like
Width="{StaticResource width} + {StaticResource margin}"
this syntax is wrong. Is there a way to specify what I need?
You can't bind to more than one source property in a Binding. Therefore you need some kind of aggregator that offers an output property you can bind against.
Here are some variations of the same pattern:
<UserControl.Resources>
<sys:Double x:Key="width">10</sys:Double>
<sys:Double x:Key="margin">30</sys:Double>
<BindableResult x:Key="widthPlusMargin" ArithmeticOperation="Add" LeftOperand="{StaticResource width}" RightOperand="{StaticResource margin}"/>
</UserControl.Resources>
<Grid Width="{StaticResource widthPlusMargin}">
with BindableResult having an implicit cast operator to double:
public static implicit operator double(BindableResult source)
{
return source.InternalResult;
}
or something like this:
<UserControl.Resources>
<sys:Double x:Key="width">10</sys:Double>
<sys:Double x:Key="margin">30</sys:Double>
</UserControl.Resources>
<Grid>
<i:Interaction.Behaviors>
<SetCombinedWidth Value1="{StaticResource width}" Value2="{StaticResource margin}"/>
</i:Interaction.Behaviors>
</Grid>
You can also google for silverlight multibinding implementations and see if that is more to your taste. But in the end it is just another variation of the aggregator.

How to check which instance of child control is used in silverlight

I have two usercontrols : ParentUserControl.xaml and ChildUserControl.xaml
ParentUserControl:
<UserControl x:Class="DemoSilverlight.ParentUserControl"
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:DemoSilverlight"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
Loaded="UserControl_Loaded">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:TabControl x:Name="tabControlHeatMapEditor" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionChanged="tabControlHeatMapEditor_SelectionChanged" Height="510" FontFamily="Verdana" FontSize="12">
<sdk:TabItem Header="Original Heat Map" x:Name="tabItemOriginalHeatMap">
<Local:ChildUserControl x:Name="chartControlOriginal"/>
</sdk:TabItem>
<sdk:TabItem Header="Revised Heat Map" x:Name="tabItemRevisedHeatMap">
<Local:ChildUserControl x:Name="chartControlRevised"/>
</sdk:TabItem>
<sdk:TabItem Header="Compare Original/Revised" x:Name="tabItemCompareOrgRev">
<!--<Local:ChildUserControl x:Name="chartControlCompare"/>-->
</sdk:TabItem>
</sdk:TabControl>
</Grid>
</UserControl>
Inside the ChildUserControl control I have a button control. In the code behind page of the
ChildUserControl.xaml.cs, I want to know which instance of the ChildUserControl is used and based on that I want to access the button within the ChildUserControl in order to make it disabled.
Can anyone help me to resolve this problem
Thanks & Regards,
Santosh Kumar Patro
Have a enum in ChildUserControl. Based on TabItem Selection set the enum property and further based on your condition you can either enable or disable the button.

how can i set header templete in pivot item at run time

actually i want to set image on header of every pivot item .....at run time using a resource
<UserControl x:Class="WindowsPhoneApplication7.heder"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="18" d:DesignWidth="24" Loaded="UserControl_Loaded">
<UserControl.Resources>
<DataTemplate x:Key="heder_image">
<Image Source="/WindowsPhoneApplication7;component/Images/appbar.feature.settings.rest.png"></Image>
</DataTemplate>
</UserControl.Resources>
it is a resource
now my pivot item are in another cs file
heder dis =new heder();
pivotItem[i] = new PivotItem();
pivotItem[i].Header = (DataTemplate)dis.Resources["heder_image"];
but the header templte r not set ......image r not display
What you're doing wrong here, is that you're setting the Header of a PivotItem to a template, instead of using the Pivot.HeaderTemplate. That way it wouldn't be necessary to generate the items in C# either, but instead databind (via. the Pivot.ItemsSource property) them, and do all the styling in XAML as you're meant to do.
And when that is said, using a image for a Pivot Header, should really be discouraged, as it goes against the platforms default look&feel, and thus against the platforms UI and UX guidelines.
Check if the image's Build Action is set to Content (see properties of the file in VS2010 explorer). If the image is in the same project as the XAML page, use "/Images/appbar.feature.settings.rest.png" instead of "/WindowsPhoneApplication7;component...".
Use this XAML snippet and set your header in XAML:
<controls:Pivot ItemsSource="{Binding MyObjects}" HeaderTemplate="{StaticResource heder_image}">
<controls:Pivot.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>

Retrieving value from static extension XAML

How do I retrieve the value (Int32.MaxValue) from the static extension:
<x:Static
x:Key="TooltipTimeout"
Member="s:Int32.MaxValue"
/>
...
<blablalba TooltipService.ShowDuration="{StaticResource TooltipTimeout}"/> <-- this does not work by the way
Methinks you're doing something else wrong. Slap this in kaxaml:
<Page
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<x:Static x:Key="Derp" Member="sys:Int32.MaxValue"/>
</Page.Resources>
<Grid>
<TextBlock
ToolTipService.ShowDuration="{StaticResource Derp}"
ToolTip="Derp" Text="Herp" />
</Grid>
</Page>
Mod tested, mother approved.
If I had to guess, I think you're not defining your xml namespace for Int32 correctly.
In WPF, You can access static member directly, like this,
<TextBlock TooltipService.ShowDuration="{x:Static s:Int32.MaxValue}"/>
However, you cannot do the same in Silverlight, as it wouldn't work. In silveright, you've to write a wrapper class, like this,
public class StaticMemberAccess
{
public int Int32Max { get { return Int32.MaxValue; } }
//define other wrapper propeties here, to access static member of .Net or your classes
}
Then do this in XAML,
<UserControl.Resources>
<local:StaticMemberAccess x:Key="SMA"/>
</UserControl.Resources>
<TextBlock TooltipService.ShowDuration="{Binding Source={StaticResource SMA}, Path=Int32Max}"/>
.
Try to bind to your resource by setting it as the Source of your binding:
{Binding Source={StaticResource TooltipTimeout}}

Datepicker Watermark

I found this answer in Stackoverflow for Removing Datepicker Watermark.
<Style TargetType="{x:Type toolkit:DatePickerTextBox}">
<Setter Property="Text" Value="Bitte wählen" />
</Style>
is it possible to set above using vb.net code.
Thank you,
Rey.
The one way I can think of to do this is using a resource dictionary.
Create a resource dictionary with that bit of XAML in it and add the dictionary to your resources on Window Initialized. This is a nice way to change styles dynamically in WPF.
In Visual Studio, add a new Resource Dictionary and make it look like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
>
<Style TargetType="{x:Type toolkit:DatePickerTextBox}">
<Setter Property="Text" Value="Test" />
</Style>
</ResourceDictionary>
Then in your window add the following to add the resource dictionary to your apps resources:
Private Sub Window1_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
Dim rd As New ResourceDictionary()
rd = CType(Application.LoadComponent(New Uri("Dictionary1.xaml", UriKind.Relative)), ResourceDictionary)
Application.Current.Resources.MergedDictionaries.Clear()
Application.Current.Resources.MergedDictionaries.Add(rd)
End Sub
If you don't want to use styles the only thing that seems to make sense then is to override the default implementation of the DatePicker and implement your own. A good description of how to do that can be found here:
http://www.tanguay.info/web/index.php?pg=codeExamples&id=144