WPF中的文本块滚动不起作用

7
我希望使我的可滚动,但我无法做到。也许问题出在中吗?
以下是代码:
 <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>

问题出在这一部分:

<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中查看,因为我不允许进行任何更改,只想要只读状态。

谢谢。

编辑:

<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>

把这段代码粘贴到你的项目中,它带有我阅读信息的RSS链接。只需看看你将得到什么。

我没有看到任何奇怪的东西。TextBlock本身将内容设置为只读,因此我建议将其放在StackPanel之外测试,看看内容是否可滚动。 - Josh
我已经尝试过了,但仍然没有任何进展... - user123_456
尝试移除StackPanel.ScrollOwner。 - Josh
我刚刚做了一个快速的WPF项目,但也无法使其工作。我会继续尝试解决它。 - Josh
好的。我刚刚尝试了你说的,但它没有起作用。 - user123_456
1个回答

10

这是我所做的事情,希望你也能做到。首先,我必须将ScrollViewer放在StackPanel周围,然后必须从TextBlock中删除Height

     <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>
抱歉,我只能理解和回答英文问题。请您提供英文版本的内容,我会尽力为您提供帮助。
    <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>

需要注意的是...我修改了你的网格行高。 *用于表示填充,数字并不表示像素。因此,你所设置的187*不会像min with resize一样行为。实际上,187*并不意味着贪婪地占据187像素,空间至少为187像素但可以根据需要增长。像我上面所做的那样将三个行的高度设置为*只是让它们各自占父容器高度的三分之一。如果你想让第二行比其他行高两倍,将其他行设置为*,将中间行设置为2*即可。由于我无法看到你的屏幕,你可以根据需要进行调整。你可能还想使用Auto并将其大小设置为内容。

这是我演示时的屏幕截图:Scrollable TextBlock


问题是我完全不懂WPF。你能把你的代码和我的代码结合起来吗?拜托了。 - user123_456
更新后的代码在编辑部分。你可能仍需要稍微调整一下。 - Josh
嘿嘿,好的,请试着运行我的代码并告诉我是否看到了什么问题,在Microsoft链接中检查这些模式图表...不是有点老,像是2006年的吗? - user123_456
也许这个项目有点老了。 - user123_456
我的还是显示2006年。那是由VS 2010自动生成的,所以我不担心它。我在我的项目中仍然看到一个可滚动的文本块。唯一不同的是我删除了绑定并使用硬编码文本。你的输出窗口中有任何绑定错误吗? - Josh
显示剩余11条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接