2012-05-24 86 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 –