WPF - implementing multilanguages applications - binding problem - wpf

Hi
I have to add support for other languages in my application. Almost entire application is ready however I have a problem with translating a tooltip which is 'loaded' from resourcesDictioanry file.
For couple element in my application I have the same tooltip. So instead of writting the same code over and over again I decied to put a tooltip into ResourceDitionary.
My tooltip looks like that
<TextBlock FontWeight="Bold" Text="Text to translation" TextAlignment="Left" />
<TextBlock Text="{Binding Path=_Code}" Grid.Column="1" TextWrapping="Wrap"/>
<TextBlock FontWeight="Bold" Text="Text to translation" TextAlignment="Left" Grid.Row="1" />
<TextBlock Text="{Binding Path=_Name}" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap"/>
<TextBlock Text="Text to translation:" Grid.Row="3" FontWeight="Bold" TextAlignment="Left" />
</Grid>
</ToolTip>
What is the best way to implement multilanguage support from ResourcesDictionary ?

You should perhaps substitute
Text="Text to translation"
by
Text="{DynamicResource TOOLTIP_TEXT_ID}"
This recipy assumes your application's translation is resource dictionary-based, of course.

Related

Adding two TextBlocks in one column of the WPF project

I want to add two TextBlocks in one column of my WPF project. I use the following codes but They locate on eachother now. How may I insert them after each other (not on each other)? Please note that I don't want to put them in two seperate columns.
<TextBlock Grid.Row="6" Grid.Column="1" x:Name="FirstName" Margin="10,10,0,0"/>
<TextBlock Grid.Row="6" Grid.Column="1" x:Name="LastName" Margin="10,10,0,0" Grid.ColumnSpan="2"/>
You can use a WrapPanel to host the TextBlocks horizontally:
<WrapPanel Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2">
<TextBlock x:Name="FirstName" Margin="10,10,0,0" Text="FirstName" />
<TextBlock x:Name="LastName" Margin="10,10,0,0" Text="LastName" />
</WrapPanel>

How to know the height of a wrapped textbox?

I need to know the height of a wrapped textBox. I am using this code:
MyView myView = new MyView();
myView.MyTextBox.Text = "my large text";
myView.UpdateLayout();
double myHeight = myView.MyTextBox.Actualheight;
No matter how large is the text, I always get 12.96 as actualheight.
If I do the same with a datagrid in which I am add new items, I am getting the right height, so I am wondering if sometimes a wrapped textbox has another behavior.
Thanks.
EDIT: i have realized that the size depends of the size of the font. At first I setted 8, but if I set 16, the actual height is the double too. So it seems that the height of the textBox depends of the font size, and it has not account if it is wrapped or not.
Running a simple test in XAML, when the text wraps, it causes the textbox ActualHeight to increase as expected as shown in the image below.
<StackPanel Orientation="Horizontal">
<TextBox Width="100" HorizontalAlignment="Left" Margin="10"
FontSize="10" Name="tbx1"
TextWrapping="Wrap" Text="Some short text"/>
<TextBlock Foreground="White" VerticalAlignment="Center"
FontSize="20"
Text="{Binding ActualHeight, ElementName=tbx1, Mode=OneWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" HorizontalAlignment="Left" Margin="10"
FontSize="10" Name="tbx2"
TextWrapping="Wrap" Text="Some text that is longer"/>
<TextBlock Foreground="White" VerticalAlignment="Center"
FontSize="20"
Text="{Binding ActualHeight, ElementName=tbx2, Mode=OneWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" HorizontalAlignment="Left" Margin="10"
FontSize="15" Name="tbx3"
TextWrapping="Wrap" Text="Short text"/>
<TextBlock Foreground="White" VerticalAlignment="Center"
FontSize="20"
Text="{Binding ActualHeight, ElementName=tbx3, Mode=OneWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" HorizontalAlignment="Left" Margin="10"
FontSize="15" Name="tbx4"
TextWrapping="Wrap" Text="Some text that is longer"/>
<TextBlock Foreground="White" VerticalAlignment="Center"
FontSize="20"
Text="{Binding ActualHeight, ElementName=tbx4, Mode=OneWay}" />
</StackPanel>
UpdateLayout doesn't actually cause anything to be rendered, it just prepares it. So until you render your view or window with myView.Show() or something equivalent, the textbox doesn't get rendered so the ActualHeight value doesn't get calculated.

WPF Custom Font Rendering

I have been given a custom font to use in our corporate application. The font itself looks fine in Word, but is rendered "elevated" by WPF. Consider the screen prints below:
When the Calibre font is rendered in WPF (I manually drew the red line):
When the Calibre font is rendered in Word:
XAML:
<StackPanel Orientation="Horizontal" Margin="10,10,0,100">
<TextBlock Text="Arial" FontFamily="Arial" FontSize="48" Padding="5,0" Margin="0" Background="AntiqueWhite" VerticalAlignment="Center" />
<TextBlock Text="Arial Bold" FontFamily="Arial Bold" FontSize="48" Padding="5,0" Margin="0" Background="AntiqueWhite" VerticalAlignment="Center" />
<TextBlock Text="Calibre Regular" FontFamily="Calibre Regular" FontSize="48" Padding="5,0" Margin="0" Background="AntiqueWhite" VerticalAlignment="Center" />
<TextBlock Text="Calibre Bold" FontFamily="Calibre Bold" FontSize="48" Padding="5,0" Margin="0" Background="AntiqueWhite" VerticalAlignment="Center" />
<TextBlock Text="Calibri Regular" FontFamily="Calibri" FontSize="48" Padding="5,0" Margin="0" Background="AntiqueWhite" VerticalAlignment="Center" />
<TextBlock Text="Calibri Bold" FontFamily="Calibri Bold" FontSize="48" Padding="5,0" Margin="0" Background="AntiqueWhite" VerticalAlignment="Center" />
</StackPanel>
Any idea? I am truly stumped on this one!
OK, I stumbled across a fix.  I opened the font in a free font editor called Type Light to verify everything looked correct in the font.  I saved the font as a new file and tried that file.  It solved the issue.  Clearly something was wrong with the OTF file.
http://www.cr8software.net/typelight.html

validating controls as a group. silverlight

I have a Silverlight page that is using MVVM. on the page I have several controls that I need to validate. for dropdowns and text boxes it is not so hard. i.e.
<TextBox Grid.Row="4" Grid.Column="1" Text="{Binding EmailAddress, Mode=TwoWay, ValidatesOnNotifyDataErrors=True}">
<i:Interaction.Behaviors>
<behaviors:UpdateOnTextChangedBehavior />
</i:Interaction.Behaviors>
</TextBox>
and I get the nice little red box with my message that I bound in the model.
but I also have a place were I have two check boxes in a stackpanel. I need to make sure one of the boxes is check. and I would like the error message to show up ether next to the stackpanal or the labeltextbox that is the 1st item in the stackpanal.
<StackPanel Grid.Row="6"
Grid.ColumnSpan="2">
<ac:LabelTextBox Content="Business Group(s):" />
<CheckBox HorizontalAlignment="Left"
VerticalAlignment="Center"
IsThreeState="False"
Content="XXX"
IsChecked="{Binding User.HasXXXUserRole, Mode=TwoWay, ValidatesOnExceptions=True}"
Margin="3"
FlowDirection="LeftToRight">
</CheckBox>
<CheckBox HorizontalAlignment="Left"
VerticalAlignment="Center"
IsThreeState="False"
Content="YYY"
IsChecked="{Binding User.HasYYYUserRole, Mode=TwoWay, ValidatesOnExceptions=True}"
Margin="3"
FlowDirection="LeftToRight" />
</StackPanel>
Thanks
Danny

Binding Textblock with Property having text "Audit" displays text "Audi"

I am binding a TextBLock with a string Property but whenever this Property contains the text "Audit", the actual text displayed on the UI is "Audi". For any other scenario, this works fine.
I am using .Net 4.0 and OS is win XP.
I am not sure if this is a bug but I can't get rid of this. If it is a bug, where do I report WPF bugs? Can anyone else also reproduce this issue? If yes, is there a workaround?
Edit: The Binding for the textblock is:
<TextBlock Padding="0,5,5,5" TextWrapping="Wrap" Text="{Binding TaskRoleMapping}" FontWeight="Bold" Foreground="White" FontFamily="Segoe UI" Margin="3,0,0,0" VerticalAlignment="Center"/>
I am sure it's not a problem with the textlength as much bigger text gets displayed alright.
Edit: The TextBlock is contained in a StackPanel
<StackPanel Height="40" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left" Width="Auto">
<ContentControl Content="ContentControl" Template="{DynamicResource vector_Summary}" Height="16"/>
<TextBlock Padding="5,5,0,5" Height="28"
Text="{Binding Path=[LABEL.MAIN.HEADER], Source={x:Static Util:Util.labelDictionary}}"
FontFamily="Segoe UI" FontSize="12"
Foreground="#FFFFFF" Margin="1,7,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="Auto"/>
<TextBlock Padding="0,5,5,5" TextWrapping="Wrap" Text="{Binding TaskRoleMapping}" FontWeight="Bold" Foreground="White" FontFamily="Arial" Margin="3,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
I like Kent Boogaart's comment/points. Basically 't' char is being cut off/out from the view. So change the font size to smaller, or remove "Bold" or introduce min size of the TextBlock, or it's parent and 't' in "Audi" will magically appear

Resources