Viewbox minimum fontsize - c#

I'm using a viewbox to scale a textblock.
But when the fontsize of the textblock is below a certain fontsize I want to trim the text with... and stop scalling.
How can I achieve this?
<Grid x:Name="UserStatusPanel" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center">
<Viewbox Name="canMain"
MaxWidth="{Binding ActualWidth, ElementName=UserStatusPanel}"
StretchDirection="DownOnly"
HorizontalAlignment="Left">
<TextBlock x:Name="UserStatusLabel"
TextWrapping="Wrap"
Text="{Binding UserStatus}"
FontFamily="Segoe UI"
FontSize="14"
Foreground="{StaticResource AlmostWhite}"
TextTrimming="CharacterEllipsis"/>
</Viewbox>
</Grid>

The only solution I can think of is taking the text out of the viewbox and binding the font size to the viewbox width or another controller value.
If you want an example look over here.
You can implement the converter with your own logics.

Related

Changing the font size of an element programmatically inside a Viewbox works weird

I need to change the font size of a label programmatically. The label is located inside a Viewbox:
<Window ...
FontSize="24"
HorizontalAlignment="Center" VerticalAlignment="Center"
WindowStyle="ThreeDBorderWindow"
ResizeMode="NoResize">
<Grid>
<Viewbox x:Name="vb" Stretch="Uniform">
<Label x:Name="label1" HorizontalContentAlignment="Center" Content="Text"/>
</Viewbox>
</Grid>
</Window>
The font size of text inside the label is assumed to be 24. However, even if I set it in my code like this to its 'original' value in xaml:
label1.FontSize = 24;
it becomes smaller.
Is there a simple way to keep the ratio between the new font size and the scale factor of the Viewbox?
You can try to set ViewBox.StretchDirection property to DownOnly, for the label will be smaller, but not bigger:
<StackPanel Width="200" >
<Label Content="Text" FontSize="24" HorizontalAlignment="Center"/>
<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
<Label Content="Text" FontSize="24"/>
</Viewbox>
<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
<Label Content="ABCDEF EEEEE ABCDEFGJKLMNOP" FontSize="24"/>
</Viewbox>
</StackPanel>
So the code results to :

How it adjust the max width of a textbox in a TreeViewItem to the edges of the container

The text box in the follow TreeViewItem, constructed by a data template, is not horizontal adjustable to the edges of the tree view.
<DataTemplate x:Key="PageTemplate" x:DataType="local:OnenoteEntity">
<TreeViewItem AutomationProperties.Name="{x:Bind Page.Title}" HorizontalContentAlignment="Stretch">
<StackPanel
MaxWidth="{Binding ActualWidth, ElementName=UiOnenoteTreeView}"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Orientation="Horizontal">
<Image
Height="{Binding ActualHeight, ElementName=UiNameOnenoteEntity, Mode=OneWay}"
Margin="0,0,10,0"
VerticalAlignment="Center"
Source="Resources/Page.png" />
<TextBlock
x:Name="UiNameOnenoteEntity"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Style="{ThemeResource BodyTextBlockStyle}"
Text="{x:Bind Page.Title}"
TextWrapping="WrapWholeWords" />
</StackPanel>
</TreeViewItem>
</DataTemplate>
The text in the box dont wrapp at the edge of the container in the tree view. The text box has the width size of the actual text, according to the following picture:
Picture
What can i do, to wrap the text? Thanks for answering
As #2zemec said, you need to change the TextWrapping property of TetxBlock to Wrap. In addition, since you set the Orientation of StackPanel as Horizontal, the StackPanel will give the full width to each child control, and then put them out horizontally. It will not limit the width of TextBlock, so the TextBlock won't wrap. In this case, you can also set a MaxWidth for TextBlock to limit it or use Grid to replace the StackPanel. For example:
<Grid MaxWidth="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Height="{Binding ActualHeight, ElementName=UiNameOnenoteEntity, Mode=OneWay}"
Margin="0,0,10,0"
VerticalAlignment="Center"
Source="Assets/StoreLogo.png" />
<TextBlock Grid.Column="1"
x:Name="UiNameOnenoteEntity"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Text="{x:Bind Title}"
TextWrapping="Wrap"/>
</Grid>
Try to change TextWrapping="WrapWholeWords" to TextWrapping="Wrap". According to MSDN
Wrap - Line-breaking occurs if the line overflows beyond the available block width, even if the standard line breaking algorithm cannot determine any line break opportunity, as in the case of a very long word constrained in a fixed-width container with no scrolling allowed.

How to set dynamic height of UIElement based on the content in text block

i need to set height of UiElement based on the content . i set content to a text block and set text block MaxWidth is some value and MaxHeight is double.MaxValue. now i set this textblock to child of border.nowi measure the border like below
textBlock.MaxWidth=200;
textBlock.MaxHeight=double.MaxValue;
var area=new Border{child=textBlock};
area.Measure(new size(textBlock.MaxWidth,textBlock.MaxHeight));
var r=area.DesiredSize;
but above code give the incorrect desiredsize for different width of text block. is there any other way to calculate the height based on text content.
Use the following
<ViewBox Width="200">
<TexBlock Text="..." ... />
</ViewBox>
It will adapt the TextBlock Size to have always a Rectangle of that Width.
Try and tell me if it the solution for your case
I think you're actually looking for the WCF Control called StackPanel, but I'll provide the code to change height programatically aswell.
Using StackPanel, in XAML:
<StackPanel Width="200">
<Border BorderThickness="1" BorderBrush="Black">
<TextBlock TextWrapping="Wrap" Text="This TextBlock will be
wrapped by a black Border that has the height of the TextBlock
and with it's Width provided from the StackPanel"/>
</Border>
</StackPanel>
Or to change TextBlock height programatically:
<Border x:Name="border" BorderBrush="Black" BorderThickness="1" Height="100" Width="200">
<TextBlock x:Name="textBlock" Background="Black" Width="40" Height="40"/>
</Border>
And then your code is simply: border.Height = textBlock.Height;
<Border Width="300" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="welcome" Foreground="Blue"/>
</Border>

C# - Windows Phone - MaxWidth & MaxHeight property being ignored

I have some Canvases in a ListBox. Inside these Canvases I have some TextBlocks which have their text bound to them. However when I try to use a MaxWidth/Height property on the text block (or the standard Height/Width), the properties are ignored! So I end up with the text overspilling the Canvas in cases.
I would much appreciate any help on the matter, and I hope I'm not just being a blind idiot!
Oh, and here's the code (the TextBlock I'm talking about is the first one):
<ListBox Margin="0,-9,-12,0" ItemsSource="{Binding}" Name="listBoxNotes" HorizontalAlignment="Left" VerticalAlignment="Top" Width="400" Height="548">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Canvas Height="175" Width="360" Background="{StaticResource PhoneAccentBrush}" toolkit:TiltEffect.IsTiltEnabled="True" Name="canvasNote" Tap="canvasNote_Tap">
<TextBlock Text="{Binding Title}" FontWeight="Bold" FontSize="26" Foreground="White" Canvas.Left="10" Canvas.Top="5" x:Name="Title" Width="100" MaxWidth="100"/>
<TextBlock Text="{Binding Details}" TextWrapping="Wrap" Width="352" FontSize="24" Foreground="White" Canvas.Left="10" Canvas.Top="35" />
</Canvas>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The problem with this is Canvas does not limit the size of their children, and especially theTextBlock control will grow as much as it needs.
Many ways to solve your problem :
Do not use TextBlock but a TextBox
Do not use Canvas but another container such as Border, Grid, StackPanel...
Try to clip the TextBlock content
<TextBlock.Clip>
<RectangleGeometry Rect="0, 0, 50, 30"/>
</TextBlock.Clip>

WPF TextBlock Underline

I have a textblock of width say 500, but my string is just say "H" but I want to underline the whole textblock width not just under H what can I do?
You should use the property "TextDecorations" of the TextBlock. Like that:
<TextBlock Text="H" TextDecorations="Underline"/>
Just to add my 2 cents, The same effect as Talia's answer can be achieved at runtime through this code:
YourTextBlock.TextDecorations = System.Windows.TextDecorations.Underline;
For some reason VS2010 doesn't show Intellisense for the RHS, but it compiles and runs correctly.
<TextBlock VerticalAlignment="Bottom"
HorizontalAlignment="Center"
Margin="40"
Height="40"
FontSize="16"
Tapped="TextBlock_Tapped"
Text="Text"
Foreground="{StaticResource LightBlue}">
<Underline>
<Run Text="Text"/>
</Underline>
</TextBlock>
Your best bet would probably be to use a Rectangle positioned immediately below the text block, whose width is always the width of the text block. Like this:
<DockPanel LastChildFill="False">
<TextBlock DockPanel.Dock="Top" x:Name="blockToUnderline" Text="H" Width="76" />
<Rectangle DockPanel.Dock="Top" Fill="Black" Height=1 Width="{Binding ElementName=blockToUnderline, Path=ActualWidth}" />
</DockPanel>

Categories