is there a possibility to prevent a textblock from resize it's parent?
I have a textblock with a lot of text and I want it to wrap, but not to enlarge the parents size. The size of the parent may be variable.
Greetings Thomas
Unfortunately there is no Property for this feature.
The only workaround that I'm aware of is to either use an existing control or place another hidden control in the same space and then bind Width of your TextBlock/TextBox to the ActualWidth of that control.
Here is an example when the TextBlock doesn't effect the Width of the ColumnDefinition but it will get wider if the ColumnDefinition is resized for another reason
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1"
TextWrapping="Wrap"
Text="{Binding ...}"
Width="{Binding ElementName=sizingControl, Path=ActualWidth}"/>
<Rectangle Name="sizingControl" Grid.Column="1" Visibility="Hidden" />
</Grid>
For this to work, you need to set a width on the parent or place another grid or some container inside the parent which holds the textblock. Then set a width on that. The textblock will not wrap on a flexible parent.
Or better yet, just set a width on the textblock.
Related
Im relatively new to WPF so I'm sorry in advance if there is a simple solution to this. However, I am trying to bind the height of a StackPanel to its containing Grid. I understand that a StackPanel automatically resizes to fit its elements but I would like to bind it to the Grid so that it does not do this. Right now the stack panel does not resize with the window since it remains the
size it needs to be in order to fit its elements.
Here is what I have so far:
<Grid x:Name="MainGrid" Grid.Row="2" Margin="1" Drop="Grid_Drop"
AllowDrop="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="175"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel x:Name="SidePanel" Grid.Column="0" Height="{Binding
ActualHeight, ElementName=MainGrid, Mode=OneWay}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition/>
</Grid.RowDefinitions>
<local:DeviceTreeView x:Name="deviceTree" PyngInfo="{Binding
PyngSystemVm.PyngInfo}" Grid.Row="0"/>
</StackPanel>
<-- There is more code here but it is not important for answering this
question -->
</Grid>
I tried binding the height of "SidePanel" to the actual height of "MainGrid" but when I run this code and inspect the elements the Grid resizes with the window but the StackPanel does not. The StackPanel and the Grid even have different heights which doesn't make sense to me as their heights should be bound together.
I have also tried wrapping the entire StackPanel in a border and binding to that but that also did not work.
You don't need to bind the height of the StackPanel, simply setting its VerticalAlignment to Stretch will do it. However... You are still not going to get what I think you want. The StackPanel only stacks its child controls, it does not adjust them (at least not in the "stack" direction). Look into using a different control like Grid or UniformGrid or just expand your existing grid to have rows as well as columns.
This time, my question is as simple as it sounds... how do you get text to wrap in a WPF TreeViewItem?
I have a simple HierarchicalDataTemplate with just one TextBlock in it.
<TextBlock Text="{Binding Value}" TextWrapping="Wrap" />
The text does not wrap.
I tried binding the Width of the TextBlock to the ActualWidth of the TreeView, but although that makes the text wrap, the Width of the TreeViewItem does not fit in the TreeView because the TreeView has Padding. Binding to the ActualWidth of the TreeViewItem has (unsurprisingly) the same effect. Another downside to this is that even the items with little text stretch outside the TreeView bounds.
<TextBlock Text="{Binding Value}" TextWrapping="Wrap" Width="{Binding ActualWidth,
ElementName=TreeView}" />
Surely there must be a better way, like somehow informing the TreeViewItem of the TreeView's bounds... I can't believe it doesn't know automatically. But how can I do this?
UPDATE >>>
Thanks to H.B.'s answer, I managed to change the Grid.ColumnSpan to 2 on the Bd Border he mentioned in the ControlTemplate and it set the width so that the text now wraps nicely. The problem is that I am using this ControlTemplate for other TreeViewItems in other TreeViews where I don't want full width items.
The solution I came up with is simple. I have bound the TreeViewItem.Tag value to the Grid.ColumnSpan property in the ControlTemplate like so:
<Border Grid.ColumnSpan="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"
Name="Bd" Grid.Column="1" ... />
This allows me to change the Grid.ColumnSpan and therefore the full width or ordinary width behaviour of the TreeViewItem by setting the TreeViewItem.Tag value to either 2 or 1 respectively.
If you look at the default template of TreeViewItems you will see a Grid like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19"
Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- ... -->
As you can see the third column takes all available space while the others are on Auto, the header is placed in the second column inside a border:
<Border Name="Bd"
Grid.Column="1"
...
This means that the column will become as large as the header, there is no restriction on it. Thus the header just gets bigger than the TreeView itself.
If you add Grid.ColumnSpan="2" to this Border it will occupy the third column as well, which is restricted by how much space is left, hence the text will wrap; this will however extend the header across the whole width which might look a bit odd when selecting it.
Of course you will also need to disable horizontal scrolling:
<TreeView ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...
Edit: I try rephrasing my question, sorry if it was not clear. Thanks to all anyway.
Say I have a UserControl whose layout has a grid with 1 row x 3 columns, the first 2 autosized and the third star-sized:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ComboBox MinWidth="80" MaxWidth="150" .../>
<CheckBox Grid.Column="1" VerticalAlignment="Top".../>
<TextBox Grid.Column="2" MaxHeight="400" TextWrapping="Wrap" HorizontalAlignment="Stretch"...>
</Grid>
The TextBox has text wrapping and its vertical scrollbar visibility set to auto and its horizontal one hidden.
Now, I have some window including a ListBox whose items are instances of this UserControl, like:
<ListBox HorizontalContentAlignment="Stretch".../>
The ListBox is in a 1-column star-sized grid and thus stretches to fit all the available width in its container. When I resize the container of this ListBox the ListBox too resizes as expected as it is stretched in a star-sized Grid column; and the same holds true for the ListBox items', which too are stretched and in this case happen to be instances of a UserControl with the above layout (3 columns in a grid, the 3rd star-sized).
The problem is that the TextBox in the 3rd column of the UserControl used as a listbox item should not automatically increase its width when I type into it some long text: it should just wrap, increasing its height (up to a maximum height; then the vertical scrollbar will appear). In other words, its MaxWidth should be determined by the available space, like its Width.
How should I code my XAML for this?
That should be the behavior by default, based on the code you've posted.
Try setting your TextBox's HorizontalAlignment="Stretch"
In WPF, I want set a controls width to be, say, 97% of it's Parent controls ActualWidth property. How can I do this?
You can use Grid panel. E.g:
<Border>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.97*"/>
<ColumnDefinition Width="0.03*"/>
</Grid.ColumnDefinitions>
<Button Content="97%"/>
<Border Grid.Column="1" Background="Green">
</Border>
</Grid>
</Border>
Grid will occupy 100% of available border's size. First column will use 97% of it. And the rest 3% are given to border in the second column.
Hope this helps.
Cheers, Anvaka
I have a grid where one columnwidth is defined as *.
The other columns are defined as Auto.
The column with the *-definition contains a usercontrol derived from Panel that also implements IScrollInfo.
During this control's MeasureOverride visibility is set to visible on a RepeatButton in another column (the RepeatButton's visibility is otherwise set to collapsed).
This does not cause the column to expand. This will only occur when I resize my window.
A simplified example:
<DockPanel LastChildFill="True">
<Grid DockPanel.Dock="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="TabItemColumn"/>
<ColumnDefinition x:Name="ScrollRightColumn" Width="Auto"/>
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="PART_ScrollViewer"
Grid.Column="0"
Margin="-1,0,0,0"
Height="32"
CanContentScroll="True"
VerticalScrollBarVisibility="Hidden"
HorizontalScrollBarVisibility="Hidden"
HorizontalAlignment="Left">
<local:TabPanel
x:Name="tabPanel"
HorizontalAlignment="Left"
IsItemsHost="True" />
</ScrollViewer>
<RepeatButton Style="{StaticResource RepeatButtonScrollRight}"
Visibility="{Binding ElementName=tabPanel, Path=CanScrollRight, Converter={StaticResource _localBooleanConverter}}"
Grid.Column="1">
</RepeatButton>
The visibility of the RepeatButton is triggered correctly, and as far as I can tell it is actually rendered, but the ActualWidth of the containing column is zero until resize.
Any ideas?
You'll need to trigger a layout change after changing the column width. Use InvalidateArrange() on the proper parent element. Take care to avoid infinite cycles.
InvalidateArrange is a good answer, but unfortunately it is silently ignored during the time a control is actually being arranged. So the trick is to call it after the arrange is complete.
This may work (I haven't tried it):
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
grid.InvalidateArrange();
}));
If this doesn't work, you might try calling InvalidateArrange and/or InvalidateMeasure on the RepeatButton, also within a Dispatcher.BeginInvoke callback.