XAML TextBlock: How to make TextBlock have variable height? - wpf

I have a ListBox that contains TextBlocks.
Sometimes the content of a TextBlock is too long and I want the height of this entry to double or triple as needed to accommodate the text.
I tried TextWrapping="Wrap" but it doesn't work. Every TextBlock is still just one line in height.
Is there an easy way to fix the problem in XAML? Thanks.
* Additional info: I tried to simplify the question but perhaps the complete scenario is better.
I have a listbox whose entries are displayed according to a template in code below.
Each entry has 2 pieces of info: a product price followed by product name.
I don't want to use the horizontal scrollbar in the listbox and want the product name to be displayed in 2 or more lines if necessary. The product name is the 2nd TextBlock.
Here's my XAML:
<ListBox Name="listBox1" ItemsSource="{Binding}" Margin="10" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock MinWidth="40" TextAlignment="Right" Text = "{Binding ProductPrice}" />
<TextBlock Text = "{Binding ProductName}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Disable the listbox horizontal scrollViewer. This way the textBlock will be forced to wrap.
XAML:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<TextBlock TextWrapping="Wrap"/>
</ListBox>
Example result:
Edit:
From the XAML you added I'm confident that the problem lays in the StackPanel.
Try replacing it with Grid for example:
<ListBox Name="listBox1" ItemsSource="{Binding}" Margin="10" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock MinWidth="40" TextAlignment="Right" Text = "{Binding ProductPrice}" />
<TextBlock Grid.Column="1" Text = "{Binding ProductName}" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
StackPanel doesn't limit the content size, therefore the textBlock doesn't knows where the space ends , and the wrapping doesn't happens.

You are using StackPanel. Try using DockPanel:
<DockPanel>
<TextBlock DockPanel.Dock="Left" MinWidth ="40" TextAlignment="Right" Text = "11.12" />
<TextBlock Text = "{Binding LongText}" DockPanel.Dock="Right" TextWrapping="Wrap" />
</DockPanel>
For Example:

this will help you do that. Dont Size the TextBlock just size the scroll viewer because texblock needs to be variable so ScrollViewer will apply Scrollbar once it goes beyond the size of ScrollViewer .
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<TextBlock/>
</ScrollViewer>
For ListBoxItem
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.Items>
<TextBlock Text="{Binding LongText}" TextWrapping="Wrap"/>
</ListBox.Items>
</ListBox>

Related

Fit a Textbox and a button in DataGrid Cell

I am trying to fit Textblock and a button in DataGrid Cell. The Textblock holds a portion of my text and when I click the button a dialog is display. I general my code looks like the one below
<DataGridTemplateColumn Header="Message" Width="Auto" MinWidth="60">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientetion="Horrizontal">
<TextBlock MinWidth="200" TextTrimming="CharacterEllipsis" Text="{Binding Text}" />
<Button DockPanel.Dock="Right" Width="90" Margin="1" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I want the Button to be always at righter side of the cell and its width to be fixed. The TextBlock needs to be variable, for example when I resize the window, and so the DataGrid, the TextBlock should stretch also.
The problem is that, I can not achieve this behaviour / view. The TextBlock varies on each DataGrid line and in some case the button is not at the righter of the cell.
I tried to change the StackPanel to Grid or DockPanel but still I can not get the desirable result.
Using Grid
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="100" Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" TextTrimming="CharacterEllipsis" Text="{Binding Text}" />
<Button Grid.Column="1" Margin="1" />
</Grid>
Any thoughts to share?
StackPanel doesn't really have the concept of aligning to the right. It stacks the elements as close as it can. You can get around this in different ways but in this case, use a DockPanel instead:
<DockPanel>
<Button DockPanel.Dock="Right" Width="90" Margin="1" />
<TextBlock MinWidth="200" TextTrimming="CharacterEllipsis" Text="{Binding Text}" />
</DockPanel>
Note that I moved the TextBlock to be the last child element of the DockPanel. DockPanel, after laying out the other child elements, allocates the remaining space to the last child element (unless you specify LastChildFill=false). In this case, we want the TextBlock to take up the remaining space.
UPDATE: based on the comments above, in addition to changing the panel type to a DockPanel (or Grid), you can use DataGridTemplateColumn.Width to a fixed value instead of Auto. This would make the column load with the specified with but the user can still modify the column with if they want to:
<DataGridTemplateColumn Header="Message" Width="60" MinWidth="60">
I'd set a static value to the DataGridTemplateColumn.Width--it can help with rendering performance. Set the size on your buttons too, so it doesn't size to fit text.
This works for me (I used the border for visualization purposes):
<Window ...
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Model}"
x:Key="VmItem">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border BorderBrush="Black" BorderThickness="1">
<TextBlock Grid.Column="0" TextTrimming="CharacterEllipsis" Text="{Binding Original}" />
</Border>
<Button Grid.Column="1" Margin="1" Content="{Binding Encoded}" MinWidth="90" MaxWidth="90"/>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Message" CellTemplate="{StaticResource VmItem}" Width="300" MinWidth="100"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Proof:

TextBlock not wrapping in Grid

I have the following XAML which is meant to show an Image and two TextBlocks on top of eachother beside it:
<StackPanel Orientation="Horizontal" >
<Image Source="{Binding CoverArt}" Height="150" />
<StackPanel Orientation="Vertical">
<StackPanel>
<TextBlock FontSize="30" Text="{Binding Title}" TextWrapping="Wrap" />
</StackPanel>
<Grid Width="auto">
<TextBlock FontSize="22" Text="{Binding Summary}" TextWrapping="Wrap" />
</Grid>
</StackPanel>
</StackPanel>
My problem is getting the text to wrap. I've tried using a Grid and assigning the columns' width but it didn't work. Neither did setting the width values to auto. The only thing that works is hard-coding the width, but I do not want that. Thanks.
A Stackpanel will stretch to the size of its content, so it's not what I would use. Use a grid, as explained in the following posts:
TextBlock TextWrapping not wrapping inside StackPanel
Text in StackPanel doesn't wrap (wp7)
TextBlock inside stackpanel does not wrap text
A quick comparison:
Before with stackpanel
After with one grid (You might want to rearrange the elements a bit)
The code for the last segment:
<pre>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Ellipse Fill="red" Width="150" Height="150" />
<StackPanel Grid.Column="1" Orientation="Vertical">
<StackPanel>
<TextBlock FontSize="30" Text="basdljhba dnaiks d., kasndca casn oiäc cas lkcnaso ca dxjwöbdq wkjöbdqw dkjwqb " TextWrapping="Wrap" />
</StackPanel>
<Grid Width="auto">
<TextBlock FontSize="22" Text="dewdewdewdewdewewd" TextWrapping="Wrap" />
</Grid>
</StackPanel>
</Grid>
Did you try setting the width of the columns to fractions instead? width="2*" That will give you some boundaries without a pixel set size. Some way or another you need to set a constraint for the container. If you have two columns and no size is set they will get 50% each. 2* will make give that column 2/3 of the total column with, see example below.

Silverlight textblock in a listbox not wrapping

I have a data bound Listbox as shown below. I want the textblock that holds the data to wrap. And I have not been able to.
What is the problem here?
Here is my code:
<DataTemplate x:Key="policyLbTemplate">
<StackPanel>
<TextBlock Text="{Binding name}" FontWeight="Bold"/>
<TextBlock Text="{Binding description}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
<ListBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Policies}"
ItemTemplate="{StaticResource policyLbTemplate}"
HorizontalContentAlignment="Stretch" />
You need to add one property to the list box: ScrollViewer.HorizontalScrollBarVisibility="Disabled", then the text will wrap (otherwise it grows offscreen). I have done it in the following code and it works fine for me.
<ListBox Name="lstbIcons" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"
VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding UserActionName}" FontWeight="Bold"/>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This is most likely because there is nothing restricting the width of the TextBlock so that it is growing offscreen. Do Horizontal scrollbars appear?
See the following related questions and try the solutions described:
WP7 TextBlock inside a ListBox not wrapping text
Force TextBlock to wrap in WPF ListBox
windows phone 7 TextBlock TextWrapping not honored in listbox
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/37236ac6-05c3-4acc-baca-abc871ba64e0

WPF - How to stop TextBox from autosizing?

I have a textbox in my visual tree as follows..
Window
Grid
ListBox
ItemTemplate
DataTemplate
Grid
Grid
Textbox...
The textbox is defined as..
<TextBox Height="Auto"
Text="{Binding Path=LyricsForDisplay}"
MinHeight="50"
MaxHeight="400"
Visibility="Visible"
VerticalScrollBarVisibility="Auto"
IsReadOnly="True"
AllowDrop="False"
TextWrapping="WrapWithOverflow">
</TextBox>
When long text is added to the bound variable (LyricsForDisplay) all of the items in the listbox expand their textboxes/grids width's to allow for the entire string to be seen if you use the scrollbar on bottom that appears...
What I would like to do is make it so the boxes/grids only resize if the user stretches the window .. NOT when a long text is entered (it could just wrap around..)
Does anyone know how to obtain the functionality?
The following works:
<ListBox Name="ListBox1"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid>
<TextBox TextWrapping="Wrap"></TextBox>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Notice the use of ScrollViewer.HorizontalScrollBarVisibility="Disabled" and TextWrapping="Wrap".
Unfortunately, the regular TextBox doesn't allow autoresize to fit the parent but NOT autoresize when the text doesn't fit.
To solve this problem, you can use a custom TextBox that reports a desired (0, 0) size. It's an ugly hack, but it works.
In your .xaml.cs file:
public class TextBoxThatDoesntResizeWithText : TextBox
{
protected override Size MeasureOverride(Size constraint)
{
return new Size(0, 0);
}
}
Then, in your .xaml file:
<Window x:Class="YourNamespace.YourWindow"
...
xmlns:local="clr-namespace:YourNamespace">
...
<local:TextBoxThatDoesntResizeWithText Height="Auto"
Text="{Binding Path=LyricsForDisplay}"
MinHeight="50"
MaxHeight="400"
Visibility="Visible"
VerticalScrollBarVisibility="Auto"
IsReadOnly="True"
AllowDrop="False"
TextWrapping="WrapWithOverflow">
</local:TextBoxThatDoesntResizeWithText>
...
</Window>
Something needs to contrain the horizontal width available to the TextBoxes, in this case you want to stop the ListBox from growing horizontally indefinitely:
<ListBox HorizontalScrollBarVisibility="Disabled"
two change the code:
1- add border tag with your grid.column and grid.row size that you are neads.
2- width and height of textbox set to it.
sample:
<Border x:Name="b" Margin="5"/>
<TextBox Height="Auto"
Text="{Binding Path=LyricsForDisplay}"
MinHeight="50"
Width="{Binding ActualWidth, ElementName=b}"
Height="{Binding ActualHeight, ElementName=b}"
Visibility="Visible"
VerticalScrollBarVisibility="Auto"
IsReadOnly="True"
AllowDrop="False"
TextWrapping="Wrap">
</TextBox>
Try setting the the MaxWidth Property in Your Textbox
<TextBox Height="Auto" Text="{Binding Path=LyricsForDisplay}" MinHeight="50" MaxHeight="400" Visibility="Visible" VerticalScrollBarVisibility="Auto" MaxWidth="100" IsReadOnly="True" AllowDrop="False" TextWrapping="WrapWithOverflow"> </TextBox>

Width of child control should match width of parent container

I'm pretty new to WPF. I often find my self struggling with getting a bunch of child controls combined width to match a given parent container.. As in the following:
<ListBox x:Name="BrugereListBox" Grid.Column="0" Grid.Row="3" DataContext="{DynamicResource Brugere}"
ItemsSource="{Binding}"
PreviewMouseLeftButtonDown="BrugereListBox_MouseLeftButtonDown"
PreviewMouseMove="BrugereListBox_PreviewMouseMove"
>
<ListBox.ItemTemplate >
<DataTemplate >
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
<StackPanel Orientation="Vertical" IsEnabled="True"
PreviewMouseLeftButtonDown="StackPanel_PreviewMouseLeftButtonDown">
<StackPanel Orientation="Horizontal" >
<Label>Navn</Label>
<TextBox Text="{Binding Name}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Password</Label>
<TextBox Text="{Binding Password}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Email</Label>
<TextBox Text="{Binding Email}"></TextBox>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In this case I'm interested in getting the width of the children of the stackpanel:
<StackPanel Orientation="Horizontal" >
<Label>Navn</Label>
<TextBox Text="{Binding Name}"></TextBox>
</StackPanel>
to match the width of
<ListBox x:Name="BrugereListBox"
Any generic solution to scenarios like this?
turns out to be a dupe of:
wpf border control to span the width of listboxItem
and the answer given there:
This is probably more to do with the
ListBoxItems themselves not taking up
the full width of the ListBox. Add the
HorizontalContentAlignment="Stretch"
attribute to your ListBox and see if
it stretches the individual items to
fill the width.
so change to:
<ListBox x:Name="BrugereListBox" Grid.Column="0" Grid.Row="3" DataContext="{DynamicResource Brugere}"
ItemsSource="{Binding}"
PreviewMouseLeftButtonDown="BrugereListBox_MouseLeftButtonDown"
PreviewMouseMove="BrugereListBox_PreviewMouseMove"
HorizontalContentAlignment="Stretch"
>
but as Kent says, using a Grid would be better in my opinion.
Use the right panel and you'll be fine. StackPanel makes its own decisions about its width/height depending on its orientation and its children. Use a Grid and it will just take up the space it is given - no more, no less.

Resources