Text stretch in WPF TextBlock - wpf

I want to stretch the text in WPF Textblock with out changing the font size of the textblock?

use a layout or render transform to scale your text in the X or Y direction depending on what you want
LayoutTransform causes the scale to be applied prior to the layout pass which means the element is rendered with the scaled size taken in to account. Whereas the RenderTransform applies the scaling after the layout pass so the element is spaced at normal size then the scale is applied.
Something like
<TextBlock Text="Foo">
<TextBlock.RenderTransform>
<ScaleTransform ScaleX="2" ScaleY="2" />
</TextBlock.RenderTransform>
</TextBlock>

To stretch text over the entire control and make it narrower, I use ViewBox and Layout Transform:
<DockPanel>
<Viewbox>
<Viewbox.LayoutTransform>
<ScaleTransform CenterX="50" ScaleX="0.5" />
</Viewbox.LayoutTransform>
<TextBlock Text="Some random text." HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Viewbox>
</DockPanel>

Related

Helix3d WPF draw text at the top/left of HelixViewport3D

I want to show some text information at the top/left of HelixViewport3D like "ShowCameraInfo" does, which display camera information at the bottom/left of the Viewport. BillboardTextVisual3D requires a 3D point, but what I want is just like TextBlock on Canvas, which just need a 2D coordinate.
I can use TextBlock, but it cannot be captured as part of HelixViewport3D.
Any idea?
Literally a Textblock placed over the HelixViewport3D should be what you need.
There is only one problem: Viewport will not recognize if you try to manipulate the camera by initiating the mouse click on the Stackpanel.
<Grid>
<hx:HelixViewport3D>
<hx:DefaultLights/>
<hx:CubeVisual3D SideLength="7"/>
<hx:CubeVisual3D SideLength="5" Fill="Red" Center="-5,3,0"/>
</hx:HelixViewport3D>
<StackPanel Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top">
<StackPanel.Background>
<SolidColorBrush Color="#FFB6B6B6" Opacity="0.4"/>
</StackPanel.Background>
<TextBlock Text="asdhfasdvfmnas" Margin="5,2"/>
<TextBlock Text="mvcbnxcvjhbkdaf" Margin="5,2"/>
<TextBlock Text="vbkjsdvj" Margin="5,2"/>
</StackPanel>
</Grid>

How to have one element in a horizontal StackPanel dictate the height of another element in the StackPanel

In a StackPanel, I would like the Height of the TextBlock to dictate the height of the Image, but trying to bind the Image Height to the TextBlock ActualHeight causes the Image to disappear. The height of the TextBlock will vary depending on the size of the font being used.
The StackPanel is defined as follows:
<StackPanel Orientation="Horizontal">
<Image Source="{Binding HyperlinkIconImagePath}"
Height="{Binding ElementName=hyperlinkTextBlock, Path=ActualHeight}"
Margin="{StaticResource IconLeftMargin}"
VerticalAlignment="Center"/>
<TextBlock>
<Hyperlink
NavigateUri="{Binding HyperlinkUri}">
<TextBlock
Name="hyperlinkTextBlock"
Text="{Binding HyperlinkDisplayedText}"/>
</Hyperlink>
</TextBlock>
</StackPanel>
I have tried many different things and none will work. Any help is greatly appreciated. Note that this StackPanel is contained in a ControlTemplate.
The solution to this issue came from a rather subtle direction. I was initially trying to bind to the ActualHeight value of the TextBlock. During the layout process, the ActualHeight value was apparently not yet determined when it was referenced by the Image Height attribute. I then thought about using the size of the font being displayed in the TextBlock, and this FontSize value apparently is already established and usable.
<Image Source="{Binding HyperlinkIconImagePath}"
Height="{Binding ElementName=HyperlinkTextBlock, Path=FontSize}"
Margin="{StaticResource IconLeftMargin}"
VerticalAlignment="Center"/>
I therefore changed the Image Height binding to reference the TextBlock font size, and this resulted in a successful rendering.
try VerticalAlignment="Center" :
<TextBloc VerticalAlignment="Center" .../>

Canvas with VisualBrush not rendering Image correctly

I've got a Canvas that I'm setting the Background property to a VisualBrush consisting of an Image. I'm data binding the Canvas' Width and Height to the Images' Width and Height. Here's the XAML:
<Canvas x:Name="imageCanvas" ClipToBounds="True" Width="{Binding ActualWidth, ElementName=dataImage}" Height="{Binding ActualHeight, ElementName=dataImage}"
Loaded="imageCanvas_Loaded" MouseMove="imageCanvas_MouseMove">
<Canvas.Background>
<VisualBrush Stretch="Uniform">
<VisualBrush.Visual>
<Image x:Name="dataImage" ClipToBounds="True" RenderOptions.BitmapScalingMode="HighQuality" Stretch="Uniform" Source="{Binding DataImage, Mode=OneWay}" LayoutTransform="{Binding DataImageLayoutTransform, Mode=OneWay}"/>
</VisualBrush.Visual>
</VisualBrush>
</Canvas.Background>
</Canvas>
Note that Image is using a LayoutTransform. I'm using this to stretch the width of the image. If I disable the LayoutTransform binding and run I get the following (properly) rendered output:
However, when I add the LayoutTransform to stretch the width of the image by a factor of 6, the image looks stretched correctly but it is too small i.e. it is not filling the Canvas uniformly:
I suspect this may have something to do with binding the Canvas Height/Width to the image height width, but it's not clear what the exact problem is. Can anyone provide some insight? Thank you in advance.

How to set textblock or label with resizable font size in WPF?

In WPF, if i put any controls in grid, if i resize the grid, it automatically resizes all the controls in it.But in label or textblock or any other text elements, all the control sizes will change but font size remains same, it will not change.
If font has to change as per grid size, What should be done?
You can achieve this by using a ViewBox. It will transform (not resize) your font (well, the control) depending on the control size.
Look at this here for more information;
<Viewbox Stretch="Uniform">
<TextBlock Text="Test" />
</Viewbox>
The following lines also give the expected result.
<Viewbox>
<TextBlock TextWrapping="Wrap" Text="Some Text" />
</Viewbox>

TextBlock filling vertical space

I want to create a TextBlock (or some other element with text in it for display only) that is vertical (-90 transform angle), but I want that element to fill up the vertical space it is contained in, but have a defined horizontal amount (I'm using vertical and horizontal terms instead of height and width since it's swapped when I have the TextBlock go vertical), and have it aligned to the left side of the container.
I believe I understand how to make a TextBlock go vertical using RenderTransform or LayoutTransform. However, I cannot seem to get the 'docking' to work properly, whenever I change the vertical aspect of the container the TextBlock increases in horizontal aspect instead of vertical.
Here is what I have:
<UserControl
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"
x:Class="AttendanceTracker.StudentView"
x:Name="UserControl" Height="172.666" Width="417.333">
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
<Border BorderBrush="Black" BorderThickness="1" RenderTransformOrigin="0.5,0.5" Background="#52FFFFFF" Width="139.667">
<TextBlock Text="My Title" TextWrapping="Wrap" FontSize="18.667" TextAlignment="Center" Foreground="White" Margin="-58.509,68.068,49.158,70.734" Background="Black" RenderTransformOrigin="0.5,0.5" Width="147.017" d:LayoutOverrides="Height">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-90"/>
<TranslateTransform/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
</Border>
</StackPanel>
Change the height of the UserControl and you will notice that the TextBlock increases in horizontal aspect instead of the desired vertical aspect.
If I understand you correctly, then this should point you in the right direction:
<StackPanel Orientation="Horizontal">
<TextBlock Background="Red" Text="My Title">
<TextBlock.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="90"/>
</TransformGroup>
</TextBlock.LayoutTransform>
</TextBlock>
</StackPanel>
The key is to use LayoutTransform, not RenderTransform. This will ensure that another layout pass occurs after the transform occurs. Otherwise, the layout system is using the original bounding rectangle to layout the TextBlock.
Beyond that, I just got rid of all the Blend-generated cruft to see what was going on. Here's the result:
alt text http://img187.imageshack.us/img187/1189/screenshottbv.png

Resources