Display non-rectangular shape TextBlock

I want to display text in a triangular area. But I can't figure out how to change the shape of the TextBlock so that the text appears in a triangular area instead of the usual rectangular area.

Here is the simplified code of my UserControl:

<Grid > <Image Height="100" Width="100" /> <!-- Some triangular image --> <TextBlock Height="100" Width="100" Text="This text should fill up the triangualr image area"/> </Grid> 
+4
source share
3 answers

It’s a good view of an overflow solution, but the contents of the inner grid will be triangular in location if something like this is done:

  <Grid> <Image Height="200" Width="200" /> <Grid Width="200"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="1" Text="Line 1" HorizontalAlignment="Center"/> <TextBlock Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" Text="This is Line 2." HorizontalAlignment="Center"/> <TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="5" Text="This is Line 3. Wud be longest." HorizontalAlignment="Center"/> </Grid> </Grid> 

For your reference, the grid is structured as follows:

Refrence image

Set a background triangular image, and I believe that is enough.

+1
source

You need to overwrite the TextBox template.

I would recommend getting a copy of Blend by extracting a TextBox template from it and modifying it to suit your needs.

If you cannot get Blend, I would recommend Show me the template , which is a WPF tool that shows the default template for most WPF controls

0
source

Just rewrite the text box template like

 <ControlTemplate TargetType="Textbox"> <Path ... define your triangle here> </ControlTemplate> 

and then set the input property in the text box to true. also align the continuation. Hor. and vert. ALG. thus, it is centered in the middle of the triangle. make sure you make the background of the text field transparent so that it does not overwrite the borders of the triangle.

also, if you do not want to go beyond, place the content presenter in the viewport.

You can also see the property of the clip.

0
source

Source: https://habr.com/ru/post/1369157/


All Articles