How to get text fit in text block in WPF - c#

I have string which i have to display in TextBlock, my TextBlock have some fixed size, i need display the text in such manner if string cannot fit in TextBlock, then i have to split the string in next TextBlock, how can i do this same.

Why don't you try using the TextWrapping property of that TextBlock?
XAML:
<TextBlock TextWrapping="Wrap" Text="very very very long text" Width="30"/>
C#:
myTextBlock.TextWrapping = TextWrapping.Wrap;

If you don't want wrapping, then slapping on a horizontal/vertical scrollbar is another option that you may want to explore. Reading the question I think textwrapping might be more appropriate (doesn't sound like you want to hide anything), but options are always nice.
<ScrollViewer Height="30">
<TextBlock Width="30" TextWrapping="Wrap">HElooooooooooooooooooooooooooooooooooooo</TextBlock>
</ScrollViewer>
EDIT: Combines a word wrap and a scrollviewer.

Related

How do I reduce the line spacing in XCeed WPF Extended Toolkit Richtextbox

I am using the WPF Extended Toolkit Richtextbox to display simple text. But the lines are widely separated rather than one after the other. Indentation appears to be working OK. It's just that 5 lines of text takes up a lot of space. I tried setting Margin="0." I cannot find a property to set such as "Line Spacing" to set.
<tools:RichTextBox Grid.Row="3"
Grid.Column="1"
Text="{Binding Procedure}"
Margin="0"
ScrollViewer.VerticalScrollBarVisibility="Auto"
BorderBrush="Gray"
BorderThickness="2"
Padding="0"
AcceptsReturn="True"
AcceptsTab="True"
AllowDrop="True"
AutoWordSelection="True"
>
<tools:RichTextBox.TextFormatter>
<tools:PlainTextFormatter/>
</tools:RichTextBox.TextFormatter>
</tools:RichTextBox>
The Document property can not be set with a binding.
Line spacing cannot be formatted using the rtf header with the Text attribute in the extended wpf richtextbox. Use the Block.LineHeight attribute instead. It has to be about 130-150% of the font size to work.
<xctk:RichTextBox Block.LineHeight="20"/>

Change text of textblock within a panoramaitem

I need to use two textblock within a single panorama item. I have used a grid to implement this.
Problem is that I need to change the text of the textblock programmatically.
<phone:PanoramaItem Header="Current Status">
<Grid >
<TextBlock x:Name="Spent" Margin="138,0,90,464" FontSize="40"/>
<TextBlock x:Name="Left" Margin="138,50,90,414" FontSize="40"/>
</Grid>
</phone:PanoramaItem>
Saying:
Spent.Text = Convert.ToString(total_spent);
Left.Text = Convert.ToString(total_left);
in the c# file gives an error.
Please tell me how to change the text of the individual textblocks :)
P.S:
I am an absolute beginner and almost completely self taught, so a simple answer will be useful
Thanks
You need to use Text property of the TextBlock to get/set the Text.
From MSDN : TextBlock.Text
Gets or sets the text contents of a TextBlock.
Try This:
Spent.Text = Convert.ToString(total_spent.Text);
Left.Text = Convert.ToString(total_left.Text);

WPF- Cut-off issue in WrapPanel

when i use more than one controls inside the wrap Panel if any control contains lengthy text (more than the window size) cut-off is happening. (See the image)
I have two textblocks and one button control.
<Grid>
<WrapPanel>
<TextBlock Text="Very long Text Message contains long text for testing " FontWeight="Bold"></TextBlock>
<Button Content="sample Text"></Button>
<TextBlock Text="sample Text textblock"></TextBlock>
</WrapPanel>
</Grid>
the cut-off happens for first text block. i want wrap to next line if the text contains more characters.
help me to solve the issue. thanks in advance.
You need the attribute TextWrapping="Wrap" in your textbox
<TextBlock
Text="Very long Text Message contains long text for testing "
FontWeight="Bold"
TextWrapping="Wrap">
</TextBlock>
Are you trying to make a single paragraph that wraps? If so, then WrapPanel isn't actually what you want.
WrapPanel takes UI elements (rectangular chunks of real estate) and lays them out left-to-right, top-to-bottom. You could enable wrapping in your first TextBlock, but then it'll take up a rectangle of screen space that's two lines high. Because the TextBlock fills that entire rectangle, the button will actually appear under it, rather than to the right of the bold words "for testing ".
If you want to make the whole thing flow like a paragraph, you don't want to use UI elements (which are always rectangular chunks); you want to use text elements (which flow in paragraphs).
The way to get text elements into your XAML is to wrap them in a TextBlock. Try this:
<Grid>
<TextBlock TextWrapping="Wrap">
<Bold>
<Run Text="Very long Text Message contains long text for testing " />
</Bold>
<Button Content="sample Text"></Button>
sample Text textblock
</TextBlock>
</Grid>
Note that I wrapped the first chunk of text in a <Run> element -- otherwise the trailing space would be ignored (assumed to be whitespace in your XAML document). The second chunk of text didn't have leading or trailing spaces, so I just put it directly inline.
This answer has a bit more about the difference between the "controls" and "text" sides of XAML.

AccessText TextWrapping not working with CheckBox

I am trying to implement a TextWrapping feature onto the text that is displayed in a CheckBox. I am using the second method in this walk through.
I have created a completely blank program to test this. I would like to make it so that, when text inside the checkBox is larger than the allowed width, it moves the excess text to the next line under checkBox.
This is my xaml:
<Grid Width="100">
<CheckBox>
<AccessText TextWrapping="Wrap"
Text="This is a really long sentence that needs to create a new line." ... />
</CheckBox>
</Grid>
The walk through uses this method to implement the result that I am looking for. Why isn't this working for me, and how do I implement it correctly?
This is what my output is:
Updates: TextWrapping changed to "Wrap"; Width removed; output updated

How to hide part of text in a richtextbox or textbox using WPF?

i would like to ask for a a way in wpf of hiding and unhiding some specific lines in a richtextbox or textbox using C# at Runtime and at the same time leave the rest of the lines visible. I would also like not to be a visible space between the visible and unvisible lines. i have an idea of selecting the lines and then change font size to 0.01, but it isnt so much elegand.
thanks in advance.
By default, you can only hide and show certain text in a WPF TextBox or RichTextBox by changing the value of the Text or RichText properties respectively. For example if you have the text "Mary had a little lamb who's fleece was white as snow" and wanted to hide the text "who's fleece was white as snow" conditonaly you would have to parse the text and remove or add "who's fleece was white as snow" in the correct location in the Text or RichText using code.
It sounds like you are just trying to modify the layout depending on certain conditions though. In that case, it is better to split the lines up into separate TextBoxes and set the visibility of those textboxes to Collapsed when certain conditions exist and set them to Visible otherwise.
If you are using TextBox you will want an outer Border which will help you match the style of TextBox, then you can set the BorderBrush and Background of the textboxes you are hiding to Transparent so it just looks like a single TextBox.
<Border
BorderThickness="1"
BorderBrush="Black"
Background="White">
<StackPanel>
<TextBox
Text="Mary had a little lamb "
BorderBrush="Transparent"
Background="Transparent" />
<!-- Set the Visisibility Property of this TextBox in code to show or hide it -->
<TextBox
Name="ConditionalTextBoxLine"
Visibility="Visible"
Text="Who's fleece was white as snow"
BorderBrush="Transparent"
Background="Transparent" />
</StackPanel>
</Border>

Categories

Resources