2012-05-24 55 views
4

我想让我的TextBlock可滚动,但我无法使它工作。也许问题在StackPanelTextBlock滚动不起作用于WPF

所以这里是代码:

<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链接粘贴到您的项目。只是想看看你会得到什么

+0

我没有看到任何奇怪。 TextBlock本身使内容只读,所以我会在StackPanel之外尝试它,看看内容是否可滚动。 – Josh

+0

我已经尝试过,但仍然没有... –

+0

尝试删除StackPanel.ScrollOwner – Josh

回答

6

这是我必须做的,希望你可以做同样的事情。首先,我必须将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> 

有些事情要指出...我修改了网格行的高度。 *用于表示填充,数字不表示像素。所以你hd = ad的行为不像min和resize。 Essentiall 187 *并不意味着一个贪婪的187像素,其中空间至少为187像素,但会根据需要增长。如上所述,将三行高度设置为*只是给它们每个父高度的三分之一。如果您希望第二行的大小是其他行的两倍,请将其他行设置为*并将中间行设置为2 *。由于我看不到你的屏幕,你可以根据需要进行调整。您可能也有兴趣使用Auto并将其设置为内容大小。

下面是它为我工作的截图: Scrollable TextBlock

+0

问题是,我是新的wpf tottaly。你能结合你的代码与我的上面?请。 –

+0

更新后的代码位于编辑部分。你仍然可能需要调整一下。 – Josh

+0

嗨似乎没有什么似乎出现在textblock –