TextBlock scroll is not working in WPF
Asked Answered
P

1

8

I want to make my TextBlock scrollable but I can't make it work. Maybe problem is in 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>

Problem is 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 part of the code should be able to scroll. I can see vertical scroll bar but can't scroll. I want to be able to see in StackPanel as I am not allowing any changes and want read only.

Thank you

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 to your project as this is with the rss link where I read info. Just to see what you will get

Presently answered 24/5, 2012 at 16:24 Comment(5)
I don't seen anything weird. The TextBlock itself makes the content readonly so I would try it outside of the StackPanel and see if the content is scrollable.Farrison
I have tried and still nothing...Presently
Try removing StackPanel.ScrollOwnerFarrison
I just made a quick wpf project and cannot get it to work either. I'll keep playing around with it.Farrison
okay. just tried what you said and it ain't working..Presently
F
10

Here's what I had to do, hopefully you can do the same. First I had to place the ScrollViewer around the StackPanel and then I had to remove the 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 point out...I modified your Grid Row Heights. * is used to denote fill and the number does not mean pixels. So what you hd=ad does not behave like a min with resize. Essentiall 187* does not mean a greedy 187 pixels, where the space is at least 187 pixels but will grow as needed. Setting the three row heights to * as I did above simply gives them each a 3rd of the parent height. If you want the 2nd row to be twice as large as the others, set the others to * and set the middle row to 2*. Since I can't see your screen, you can adjust as needed. You may also be interested in using Auto and size it to the content.

Here's a screenshot of it working for me: Scrollable TextBlock

Farrison answered 24/5, 2012 at 16:56 Comment(16)
Problem is that I am tottaly new to wpf. Can you combine your code with mine above? please.Presently
Updated code is in the Edit section. You still may need to tweak it a bit.Farrison
hi nothing is seems to be appearing in the textblockPresently
I didn't realize there was a RowSpan of 2 on the StackPanel located in Grid.Row 1. I updated the code.Farrison
Still nothing, I can't see textblock area at all. Everything else is loading, only that part doesntPresently
Try what's there now. I just loaded it and it's working. One thing I did notice is in my copy and paste I accidentally malformed the xaml. I'm surprised you didn't see a build error from it.Farrison
Another thing to consider is I have this in Grid.Row=2. I don't know what your surrounding xaml looks like, so you may need to play with the grid row heights and container structure to ensure everything is visible.Farrison
yes I removed an backslash sign that you had inside. Code is not so big so I'll post it in the edit section. It is a already written class for rss reading. But as you see it's not working. I will paste updated code from youPresently
Just tried what you posted and it's working here. What elements are wrapping your grid element? Are you sure you're not having any binding errors?Farrison
Sorry. I must have been looking at my own when I wrote that.Farrison
hehe okay, please try my code and tell me if you see something, check this schemas in the microsoft link above...isn't that a bit old like 2006Presently
maybe this project is a bit oldPresently
Mine still say 2006 as well. That's auto-generated by VS 2010 so I'm not worried about it. I'm still seeing a scrollable texblock in my project. The only difference in content is I removed the bindings and use hardcoded text. Do you have any binding errors in your output window?Farrison
I just added an image of it working locally. The only thing I can think of now is the data is empty.Farrison
let us continue this discussion in chatPresently
You missed the DataContext="{Binding ElementName=lstItems, Path=SelectedItem}" in <ScrollViewer Grid.Row="2".Zeigler

© 2022 - 2024 — McMap. All rights reserved.