I have a ListView and in it's ItemTemplate there's a TextBlock (inside a Grid) which often has long lines of text. The problem is if the text is too long it increases the width of the ListViewItem rather than wrapping the text. How can I limit the width of the TextBlock so that it will not exceed the width of the ListView?
I don't want to hardocde the width to a constant value.
I tried setting the ScrollViewer.HorizontalScrollBarVisibility property to Disabled and setting TextWrapping="Wrap" on the TextBlock, but that didn't do the trick.
When I debug the application the Live Property Explorer shows that even though the ScrollViewer.HorizontalScrollBarVisibility is disabled it's still horizontally scrollable (the IScrollProvider.HorizontallyScrollable property is true).
Any idea how I can limit the textblock size properly?
Played with it a little and this gave me the expected result:
<ListView x:Name="listView"
HorizontalAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=.}" TextWrapping="Wrap"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Hope it works for you too!.
If you don't want to hard code the MaxWidth for the text block, just give a relative width to it based on the ListView width.
Related
I want to create a layout(or panel) for tooltip that has a flexible height with fixed width like the image that I mentioned
But in my xaml code both height and width is fixed !
<TextBox Name="mytxt" TextChanged="mytxt_TextChanged" Height="102"
SpellCheck.IsEnabled="True" >
<TextBox.ToolTip>
<ToolTip Background="#e74c3c">
<StackPanel >
<Label Content="This is a error test for textbox with flat red background color"/>
</StackPanel>
</ToolTip>
</TextBox.ToolTip>
</TextBox>
Posting my comment as an answer since it solved your issue:
It looks like using the Label could be the issue. Replace the Label with a TextBlock. Then define the TextWrapping property to true. Set a fixed width and that will give you the desired behavior you are looking for.
I have faced a problem and tried the internet and threads here to find a solution but haven't found yet.
I am a beginner in programming any help will be appreciated.
I have a window which will be displayed on the secondary screen and a TextBlock wrapped in a viewbox and the text is binded and will be populated dymnamically on runtime on the second screen. The fontsize is also Binded and can be changed by the user.
This is working perfectly well.
The Problem Is That when the user increases fontsize to a higher value the text looks good if the text is short but if the text is very very large the text looks ugly sometimes becomes unreadable.
I have tried the ViewBox's Sretch direction and stretch but haven't found a better way to solve my problem but manually if there is very very long text and the text is unreadable reducing the fontsize makes text readable.
How may i be helped. One solution I have in mind is to calculate the fontsize maybe on height and width of parent grid such that when the text is very long the calculation reduces the fontsize to where it becomes readable.
here is my sample Xaml
<Grid>
<Viewbox Stretch="Fill"
StretchDirection="DownOnly">
<TextBlock Text="{Binding}"
Width="{Binding}"
FontSize="{Binding}"
TextWrapping="Wrap">
</TextBlock>
</Viewbox>
</Grid>
Here is the Ugly Behavior when text is very long and fontsize is set highly
Expected behavior. Here the fontsize is manually set lower
Okay I found a work around the problem. If anyone has another solution it will be welcomed.
I wrapped the textblock inside a border and removed binding of the width of the textblock and I will be increasing and decreasing the width. If font size increases I will decrease the width by a ratio in the ViewModel and vice versa.
<Grid>
<Viewbox >
<Border>
<TextBlock Text="{Binding}"
Width="{Binding}"
FontSize="{Binding}"
TextWrapping="Wrap">
</TextBlock>
</Border>
</Viewbox>
I'm trying to add a Scrollbar for each TabItem so when there is too much content i can scroll down. I've tried to set it to visible but it doesn't work.
The TabItems will be added dynamically over the code with Header and Content.
Any ideas to add a Scrollbar horizontally and vertically over code or XAML?
<TabControl x:Name="SemesterTabs" Margin="20,61,20,55" Width="584" HorizontalAlignment="Left">
<TabControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<!-- Tabs mit Datagridview binden und nur ein Eintrag in den Tabs auswaehlbar machen -->
<DataGrid IsReadOnly="True" SelectionMode="Single" FontStyle="Normal" ItemsSource="{Binding DefaultView}" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" SelectionChanged="DataGrid_SelectionChanged">
<!-- Um Faecher mit Rechtsclick einfuegen, bearbeiten und loeschen zu koennen -->
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Hinzufügen" Click="addNewRow_Click"/>
<MenuItem Header="Bearbeiten" Click="editRowEntry_Click"/>
<MenuItem Header="Löschen" Click="deleteRowEntry_Click"/>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
</StackPanel>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
The StackPanel is the problem, you have to remove it.
To understand why, it helps to think of panels and container controls as containers that have an external size, i.e. the size they take up in their container, and an internal size, i.e. the space they provide for their children.
For example, in a Grid, the external size and the internal size are the same. But in a ScrollViewer, the internal size may be infinite, while its external size is not.
The same is true for a StackPanel, say with a vertical orientation. The internal width of such a panel is the same as its external width, but its internal height is infinite.
Now, when you place a ScrollViewer in such a panel, it may present a horizontal scrollbar, as needed to provide space for its content, but it will never present a vertical scrollbar. It doesn't need to, as its external height can grow to infinite, and because of that, so can its internal height without having to use a scrollbar.
The Problem was the Stackpanel. Somehow it was blocking the Scrollviewer in my TabItems.
I have a grid that further down have a StackPanel. I have defined the row's height for the last to be "*", and in this very last row, is where the StackPanel and all the control would inhabit.
So I have the bellow code in XAML for my StackPanel
<StackPanel Grid.Row="1" MaxHeight="333">
<StackPanel MaxHeight="333">
<ScrollViewer MaxHeight="333">
<TextBlock x:Name="lblRouteDetail" FontSize="35" TextWrapping="Wrap"/>
</ScrollViewer>
</StackPanel>
</StackPanel>
Well, it worked, only that I have to constraint that the MaxHeight is 333, without that, it won't work; the ScrollViewer won't work, the content in the TextBlock wouldn't be scrollable.
Could you state where is my problem, and how to fix this things up?
A StackPanel, unless set to a specific height (or width if its orientation is set to Horizontal), does not constrain the height of its children, but is sized according to them. If you want to scroll your controls, you could either keep the MaxHeight property or use a different panel for holding them, such as a Grid or a DockPanel.
Imagine this. I've got a Border which contains some custom wpf control, lets call it MyControl. This Border stretches itself when window is resized (to fill available space). MyControl size is fixed. Now, I want my control to have HorizontalAlignment="Center" when it fits the available space, and HorizontalAlignment="Left" when it doesn't. I'm having trouble figuring out how to implement such behaviour though.
I guess, i can subscribe to Border's SizeChanged event and change alignment in code-behind depending on ActualWidths of Border and MyControl, but isn't there an easier way? Can this be achieved by databinding or by attached behaviour?
It will automatically behave like that if you set the control's Width and Height to fixed values and HorizontalAlignment and VerticalAlignment to Stretch instead of Center:
<Border BorderBrush="Red" BorderThickness="5">
<my:MyControl Width="200" Height="150"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Border>