WPF Label Justify & Distribute Text - c#

I have a Label just displaying a title.
How can I justify the text? and how can I distribute the text? Can I use a label or do I have to use another control?

Label doesn't have any text alignment property, however TextBlock does have text alignment properties so you can use justify if that's what you're looking for:
<TextBlock TextAlignment="Justify" />

As Matt said you can only apply the Justify property on TextBlock, so the way to go is to put the TextBlock inside of the Label as below:
<Label
Grid.Row="2"
Grid.Column="2"
Grid.ColumnSpan="4"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="26">
<TextBlock
TextWrapping="Wrap"
Text="Sample Text"
TextAlignment="Justify"/>
</Label>

Related

WPF vertical scrollbar aligns with text and not with textbox width

For an application written in C# using WPF I have the following issue:
I have a textbox with text that is being pulled from a database. The length and format of the text can vary from being a short remark, to a long text of multiple paragraphs. In some edge cases, we get text which consists of many short lines 1. Because space is limited vertically and horizontally, we need to define the dimensions of the textboxes to fit the UI 1. As you can see, in the case where the textbox has to display more than 5 lines, with the lines being very short, we get a situation where the scrollbar doesn't align with the right side of the textbox, but aligns with the text itself.
I have tried to put the textbox inside of a Scrollviewer and tried to fix the issue this way, but this resulted in the scrollbar being outside of the textbox, with the thumb of the scrollbar not showing and the dimensions of the scrollbar itself being completely off.
The final result should be a scrollbar that is always aligned to the right side of the textbox, no matter the length of the text that is being displayed.
Examples of textboxes, top one being an edge case which comes up sometimes
<Label Grid.Row="0"
Content="{StaticResource NominationInfo_OperatorRemarks}"
Style="{StaticResource UnderlineLabel}" />
<TextBox Width="500"
Grid.Row="1"
HorizontalAlignment="Left"
MaxLines="5"
IsReadOnly="True"
Text="{Binding OperatorRemarks, Mode=OneWay}"
TextWrapping="Wrap"
ScrollViewer.VerticalScrollBarVisibility="Auto">
</TextBox>
<Label Grid.Row="2"
Content="{StaticResource NominationInfo_LoadingInstructions}"
Style="{StaticResource UnderlineLabel}" />
<TextBox Grid.Row="3"
MaxLines="5"
IsReadOnly="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Text="{Binding OperatorInstructions, Mode=OneWay}"
TextWrapping="Wrap"
Width="500"
HorizontalAlignment="Left" />
I suppose you have confounded HorizontalAlignment and HorizontalContentAlignment.
Putting the following code to the TextBox should do the trick.
HorizontalAlignment="Stretch" HorizontalContentAlignment="Left"

How do I wrap the content of a textbox inside a scrollviewer so that it scrolls when full in WPF?

I have placed a textbox inside a scrollviewer control. The textbox is updated with content from my database, however when the box becomes full, the box resizes to fit more text. I would like the scrollviewers scroll feature to activate at this point, and allow the user to scroll to see more text. I have tried the following:
<ScrollViewer>
<TextBox FontSize="14" TextWrapping="Wrap" ScrollViewer.CanContentScroll="True"/>
</ScrollViewer>
<ScrollViewer>
<TextBox FontSize="14" TextWrapping="Wrap" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Visible" AcceptsReturn="True"/>
</ScrollViewer>
Any help would be much appreciated, Thanks.
You can use the ScroollViewer attached properties directly on a TextBox. Specify also the maximum height of the TextBox - MaxHeight:
<TextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True"
FontSize="14" TextWrapping="Wrap" MaxHeight="300" />
No need to insert the TextBox inside the ScrollViewer.
you can implement scrolling functionality on a content control like

WPF Button Font Awesome

I want to Add an Icon and then Aside the Button text, But when I add the fontAwesome Icon the text disapears, here my Button WPF code:
<Button fa:Awesome.Content="Adjust" Content="My Text" Background="#23282D" Foreground="#F1F1F1" FontSize="16" HorizontalContentAlignment="Left" Padding="10" BorderThickness="0" Height="44" />
The Icon is here but the Text not, When I remove the Icon the text comes back again. I tried to play with Alignment and Searching here and on other Forums but can't figure out why this not work. Any ideas what I'm doing wrong?
The simplest way is to set the content of the button to a container (e.g. Grid or StackPanel) containing a TextBlock for the caption and an ImageAwesome control for the icon, setting the appropriate properties of each control as required.
<Button>
<StackPanel Orientation="Horizontal">
<fa:ImageAwesome Icon="Adjust" />
<TextBlock Text="My Text" />
</StackPanel>
</Button>

i want to replace these textbox with place holder text in textbox in wpf

In my project is having the below code for textbox i want to replace these textbox with place holder text in textbox in wpf.
<TextBox Grid.Column="0" MaxLength="1000" TextWrapping="Wrap" AcceptsReturn="True"
Grid.Row="1"
Margin="5,0,0,5" Height="100"
Text="{Binding QuestionText, UpdateSourceTrigger=LostFocus}" />
I didnt get the language but hopefully you wanted to bind it this way
<TextBox Grid.Column="0" MaxLength="1000" TextWrapping="Wrap" AcceptsReturn="True"
Grid.Row="1"
Margin="5,0,0,5" Height="100"
PlaceholderText="{Binding QuestionText, UpdateSourceTrigger=LostFocus}" />
what you can do is either extend the TextBox control on your own or use what worked for me:
Watermark TextBox
read the instructions at the link and implement accordingly

How can I format text in a TextBlock?

I am trying to make a single block of text composed of multiple TextBlocks
- THE TARGET
For example, I want to achieve like the line below:
"I read this line in a ListBox, notice the multiple text formatting"
The way I'm trying to do is
<StcakPanel Orientation="Horizontal" Width="400" >
<TextBlock Text="I read this line in a " TextWrapping="Wrap" />
<TextBlock Text="ListBox" FontStyle="Italic" TextWrapping="Wrap"/>
<TextBlock Text=", notice the multiple " TextWrapping="Wrap" />
<TextBlock Text="text formatting" FontWeight="Bold" TextWrapping="Wrap"/>
<StcakPanel>
- THE PROBLEM
The text does not fit in the StackPanel, despite of setting TextWrapping for TextBlocks and Width for StackPanel.
I want to generate this code at runtime. I don't know how many words do I need to format.
Kind of showing SearchResults with highlighted search keywords.
- THE QUESTION
How can the above target be achieved either using StackPanel or something else? Having the following constraints.
The text length is unknown
The number of textblocks in the stackpanel is unknown
width and height is Unknown
Thanks very much
You should use a single <TextBlock> which can contain multiple <Run>s that can each have their own formatting. If you want to insert a linebreak, you can use the <Linebreak /> control.
<StackPanel Orientation="Horizontal" Width="400" >
<TextBlock>
<Run Text="I read this line in a" />
<Run Text="ListBox" FontStyle="Italic" />
<Run Text=", notice the multiple" />
<Run Text="text formatting" FontWeight="Bold" />
</TextBlock>
<StackPanel>
At that point you probably don't even need the <StackPanel> unless you are going to have multiple <TextBlocks> stacked on top of one another.
See this post for more information and examples: http://www.danderson.me/posts/working-with-the-wpf-textblock-control/
To databind multiple runs within a TextBlock, see this answer: Databinding TextBlock Runs in Silverlight / WP7

Categories