Scrolling TextBlock not working in WPF

I want to scroll a TextBlock , but I cannot get it to work. Maybe there is a problem in the StackPanel ?

So here is the code:

  <Grid Margin="3"> <Grid.RowDefinitions> <RowDefinition Height="152*" /> <RowDefinition Height="86*" /> <RowDefinition Height="67*" /> </Grid.RowDefinitions> <ListBox x:Name="lstItems" Margin="3,6,3,0" ItemsSource="{Binding Source={StaticResource rssData}}" SelectedIndex="0"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Width="20" Margin="3" Source="{Binding XPath=enclosure/@url}" /> <TextBlock Margin="3" VerticalAlignment="Center" Text="{Binding XPath=title}" FontWeight="Bold" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <StackPanel Grid.Row="1" Orientation="Vertical" DataContext="{Binding ElementName=lstItems, Path=SelectedItem}" Grid.RowSpan="2"> <StackPanel.ScrollOwner> <ScrollViewer /> </StackPanel.ScrollOwner> <TextBlock Margin="3" FontSize="13" FontWeight="Bold" Text="{Binding XPath=title, Path=InnerText}" /> <TextBlock Margin="3" Opacity="0.72" Text="{Binding XPath=pubDate}" /> <ScrollViewer> <TextBlock ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="3" FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" Height="216" Width="489" AllowDrop="False" Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" /> </ScrollViewer> </StackPanel> </Grid> 

The problem in this part:

 <ScrollViewer> <TextBlock ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="3" FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" Height="216" Width="489" AllowDrop="False" Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" /> </ScrollViewer> 

This piece of code should be scrollable. I see a vertical scroll bar, but I can’t scroll it. I want to see in the StackPanel , as I do not allow any changes and want only read.

thanks

EDIT:

 <Window x:Class="RssDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RSS Demo" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="576" Width="521"> <Window.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFE2E2E2" Offset="0" /> <GradientStop Color="White" Offset="1" /> </LinearGradientBrush> </Window.Background> <Window.Resources> <XmlDataProvider x:Key="rssData" XPath="//item" Source="http://www.hak.hr/rss/stanje/hr.xml" /> </Window.Resources> <Grid Margin="3"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ListBox x:Name="lstItems" Margin="3,6,3,0" ItemsSource="{Binding Source={StaticResource rssData}}" SelectedIndex="0"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Width="20" Margin="3" Source="{Binding XPath=enclosure/@url}" /> <TextBlock Margin="3" VerticalAlignment="Center" Text="{Binding XPath=title}" FontWeight="Bold" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <StackPanel Grid.Row="1" Orientation="Vertical" DataContext="{Binding ElementName=lstItems, Path=SelectedItem}"> <TextBlock Margin="3" FontSize="13" FontWeight="Bold" Text="{Binding XPath=title, Path=InnerText}" /> <TextBlock Margin="3" Opacity="0.72" Text="{Binding XPath=pubDate}" /> </StackPanel> <ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> <TextBlock ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="3" FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" Width="489" AllowDrop="False" Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" /> </ScrollViewer> </Grid> </Window> 

Paste this code into your project, as it is linked to the rss link where I read the information. Just to find out what you get

+4
source share
1 answer

That's what I had to do, hope you can do the same. First I had to place the ScrollViewer around the StackPanel , and then I had to remove Height from the TextBlock .

  <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> <StackPanel Orientation="Vertical"> <TextBlock Text="Test" /> <TextBlock x:Name="test" Margin="3" FontStyle="Italic" VerticalAlignment="Stretch" TextWrapping="Wrap" TextAlignment="Justify" Width="489" AllowDrop="False" Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" /> </StackPanel> </ScrollViewer> 

EDIT

  <Grid Margin="3"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ListBox x:Name="lstItems" Margin="3,6,3,0" ItemsSource="{Binding Source={StaticResource rssData}}" SelectedIndex="0"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Width="20" Margin="3" Source="{Binding XPath=enclosure/@url}" /> <TextBlock Margin="3" VerticalAlignment="Center" Text="{Binding XPath=title}" FontWeight="Bold" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <StackPanel Grid.Row="1" Orientation="Vertical" DataContext="{Binding ElementName=lstItems, Path=SelectedItem}"> <TextBlock Margin="3" FontSize="13" FontWeight="Bold" Text="{Binding XPath=title, Path=InnerText}" /> <TextBlock Margin="3" Opacity="0.72" Text="{Binding XPath=pubDate}" /> </StackPanel> <ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> <TextBlock Margin="3" FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" Width="489" AllowDrop="False" Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" /> </ScrollViewer> </Grid> 

Some things to indicate ... I changed your grid heights. * is used to indicate padding, and the number does not mean pixels. So you hd = ad does not behave like min with resizing. Essentiall 187 * does not mean greedy 187 pixels, where the space is at least 187 pixels, but will grow as needed. Setting the three line heights to *, as I did above, just gives them every third parent height. If you want the second row to be twice as large as the rest, set the rest to * and set the middle row to 2 *. Since I do not see your screen, you can adjust it as needed. You may also be interested in using Auto and the size of its contents.

Here is a screenshot of this working for me: Scrollable TextBlock

+6
source

Source: https://habr.com/ru/post/1414194/


All Articles