WPF- Cut-off issue in WrapPanel - c#

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.

Related

Highlight part of text in textblock not working on all listboxItems

I'm trying to highlight part of text in a textblock from a listbox datatemplate which in turn is bounded to a property of a custom class by using a textbox to search the list for input text. But the problem is that only part of the items are highlighting (most of the ones visible) but when i maximize the window and try to input another character then suddenly all of them gets highlighted my guess where the problem might be is in this piece of code:
ListBoxItem listboxItemFound= (ListBoxItem)this.listBox1.ItemContainerGenerator.ContainerFromItem(TItem);
Since this method is returning a null when the items are not visible but the items are currently in the listbox. Somehow I guess the items listboxItem instances are not yet created until you scroll down or maximize to view more items.
XAML DataTemplate:
<DataTemplate>
<Grid Name="gridOfListbox" Height="25" Margin="0,2">
<DockPanel Name="dockpanelWithTxtBlock">
<TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
<Run Text="" /><Run Background="Yellow" Text="" /><Run Text="{Binding ProductID}" />
</TextBlock>
</DockPanel>
</Grid>
</DataTemplate>
If more code is needed just let me know.
Any help would be greatly appreciated!!
Also if there is any other better way of finding the listboxItem bounded to the custom Item just let me know. Thank you very much!
[Pic of problem] http://i.stack.imgur.com/HViag.png
One way to fix this is to set VirtualizingStackPanel.IsVirtualizing to false for your ListBox. This will cause all of the items to be created right away. The downside to this is if your ListBox has many items, your program will use more memory (since more items will be created), and could potentially run slower depending on the number of items.
A better solution to consider would be to have multiple DataTemplates for this - one without the highlight, and one with. You can set a DataTemplateSelector for your ListBox (using the ItemTemplateSelector property). The selector can choose which template to use based on if the item matches the search term or not.
The tricky part would be writing the template with the highlighted text. You could probably achieve that by having properties on the object the ListBoxItem is bound to for the text before the highlighted text, the highlighted text, and then the remaining text.

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>

Character encoding for use within ItemTemplate

I'm creating a menu from a ListBox. I'm using FontAwesome to create some font-icons. This is part of the ListBox ItemTemplate.
<TextBlock FontFamily="FontAwesome" VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="32" Text="{Binding MenuCode}"
ToolTip="{Binding Tooltip}" >
The problem resides within the TextBlock's Text. I need to display the symbol, not the menu code. So, for example, if I use Text="" directly, then the music icon appears (fixed for all the items), but when I use DataBinding (each item has a different symbol): Text="{Binding MenuCode}" then the text  (that is, the menu code as string) appears (as text, no icon). I guess problem is related with encoding, but can't fix it. Any idea?
Wrong escape sequence. HTML uses '#&x' while C# uses \u. So your "#&xF001" would become "\uF001"

How to get text fit in text block in WPF

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.

Categories

Resources