2012-05-25 76 views
4

我想讓我的文本塊區域可滾動,因爲有很多文本要加載。如何使TextBlock在WPF中可滾動

我會把代碼的al,因爲它不是太大,所以你可以測試它。

<UserControl x:Class="Fleet_management.Info" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" Height="492" Width="578"> 
    <UserControl.Background> 
     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
      <GradientStop Color="#FFE2E2E2" Offset="0" /> 
      <GradientStop Color="White" Offset="1" /> 
     </LinearGradientBrush> 
    </UserControl.Background> 

    <UserControl.Resources> 
     <XmlDataProvider x:Key="rssData" XPath="//item" Source="*******" /> 
    </UserControl.Resources> 

    <Grid Margin="3" Height="598" Width="565"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="252*" /> 
      <ColumnDefinition Width="90*" /> 
      <ColumnDefinition Width="223*" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="177*" /> 
      <RowDefinition Height="55*" /> 
      <RowDefinition Height="122*" /> 
      <RowDefinition Height="177*" /> 
     </Grid.RowDefinitions> 

     <ListBox x:Name="lstItems" Margin="3,0" ItemsSource="{Binding Source={StaticResource rssData}}" 
       SelectedIndex="0" VerticalAlignment="Stretch" FontStretch="Normal" FontSize="14" FontFamily="Lucida Sans Unicode" Grid.ColumnSpan="3"> 
      <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}" Margin="0,0,0,5" Grid.ColumnSpan="3"> 
      <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" DataContext="{Binding ElementName=lstItems, Path=SelectedItem}" Margin="0,0,3,115" Grid.RowSpan="2" Grid.ColumnSpan="3"> 
      <TextBlock ScrollViewer.CanContentScroll="True" Margin="3" 
         FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" Width="528" AllowDrop="False" 
         Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" Height="215" FontSize="14" Padding="0,0,5,0" /> 
     </ScrollViewer> 

    </Grid> 
</UserControl> 

這是無法正常工作的一部分:

<ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" DataContext="{Binding ElementName=lstItems, Path=SelectedItem}" Margin="0,0,3,115" Grid.RowSpan="2" Grid.ColumnSpan="3"> 
      <TextBlock ScrollViewer.CanContentScroll="True" Margin="3" 
         FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" Width="528" AllowDrop="False" 
         Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" Height="215" FontSize="14" Padding="0,0,5,0" /> 
     </ScrollViewer> 

代碼加載文本,但不是全部的話,應該有滾動

回答

5

只需從TextBlockScrollviewer刪除WidthHeight,它應該工作。順便說一下:不需要ScrollViewer.CanContentScroll="True"

+0

OMG我真的不敢相信這是一個問題。爲你做好準備! –

+0

如果我可以給你投票100次,我會:) –

0

這些屬性添加到的ScrollViewer標籤。允許滾動TextBlock的內容。

CanContentScroll =「True」

+1

我已經把它,它不工作..你可以嘗試這個例子在你的項目 –

0

這是您更改的xaml代碼。它的工作正常。

我改變了根據我的數據的綁定。您需要使用您的數據成員進行更改。

<Grid Margin="3"> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="177*" /> 
      <RowDefinition Height="100*" /> 
      <RowDefinition Height="122*" /> 
      <RowDefinition Height="177*" /> 
     </Grid.RowDefinitions> 

     <ListBox x:Name="lstItems" 
       Grid.ColumnSpan="3" 
       Margin="3,0" 
       VerticalAlignment="Stretch" 
       FontFamily="Lucida Sans Unicode" 
       FontSize="14" 
       FontStretch="Normal" 
       SelectedIndex="0"> 
      <ListBox.Background> 
       <ImageBrush /> 
      </ListBox.Background> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Width="100" 
            Margin="3" 
            Text="{Binding Name}" /> 
         <TextBlock Margin="3" 
            VerticalAlignment="Center" 
            FontWeight="Bold" 
            Text="{Binding InceptionDate}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

     <StackPanel Grid.Row="1" 
        Margin="5" 
        DataContext="{Binding ElementName=lstItems, 
              Path=SelectedItem}" 
        Orientation="Vertical"> 
      <TextBlock Margin="3" 
         FontSize="13" 
         FontWeight="Bold" 
         Foreground="Red" 
         MaxHeight="50" 
         Text="{Binding FundSummary}" 
         TextWrapping="Wrap" /> 
      <TextBlock Margin="3" 
         Foreground="Blue" 
         Opacity="0.72" 
         ScrollViewer.CanContentScroll="True" 
         ScrollViewer.VerticalScrollBarVisibility="Auto" 
         Text="{Binding ManagerBio}" 
         TextWrapping="WrapWithOverflow" /> 
     </StackPanel> 
     <ScrollViewer Grid.Row="2" 
         Grid.RowSpan="2" 
         Grid.ColumnSpan="3" 
         Margin="10" 
         DataContext="{Binding ElementName=lstItems, 
              Path=SelectedItem}" 
         HorizontalScrollBarVisibility="Disabled" 
         VerticalScrollBarVisibility="Auto"> 

      <TextBlock Height="Auto" 
         Margin="3" 
         AllowDrop="False" 
         Background="Silver" 
         FontFamily="Lucida Sans Unicode" 
         FontSize="14" 
         FontStyle="Italic" 
         Foreground="YellowGreen" 
         Padding="0,0,5,0" 
         ScrollViewer.CanContentScroll="True" 
         Text="{Binding ManagerBio}" 
         TextAlignment="Justify" 
         TextWrapping="Wrap" /> 

     </ScrollViewer> 
    </Grid> 
+0

這不適合我...你可以嘗試我的代碼或把我的綁定在你的內部,因爲我什麼都不能做加載 –

1

創建一個可滾動文本框

直到微軟想出了一個解決方案,使TextBlock控件滾動,以下解決方案完美的作品。 ``` 創建一個定義單個字符串的類;例如(solutions文件夾混凝土中創建的類):

namespace jScheduleVI.Concrete 
{ 
    public class MyString 
    { 
     public string MyParagraph { get; set; } 
    } 
} 

在後面的代碼對你要顯示的滾動文本的頁面,聲明上面創建的單串類項目的一個ObservableCollection:

public ObservableCollection<MyString> textList = new ObservableCollection<MyString>(); 

創建一個或多個按鈕來激活Xaml中可滾動文本的顯示,以顯示其將顯示的頁面。例如是關於按鈕和隱私政策按鈕(I置於Grid.Row =」 0」 的網格內的這些按鈕):

<RelativePanel>    
      <Button Name="InformationButton" 
        ToolTipService.ToolTip="About" 
        RelativePanel.AlignRightWithPanel="True" 
        Margin="5" 
        Background="White" 
        Click="InformationButton_Click" 
        Width="50" Height="30" 
        HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch"> 
       <Image Source="Assets/InfoIcon.jpg" 
          Stretch="UniformToFill"/> 
      </Button> 
      <Button x:Name="PrivacyPolicy" 
        RelativePanel.LeftOf="InformationButton" 
         ToolTipService.ToolTip="Privacy Policy" 
        Margin="0,5,0,5" 
        Background="White" 
        Click="PrivacyPolicy_Click" 
        Width="40" Height="30" 
        HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch"> 
       <Image Source="Assets/Icons/Privacy.png" 
          Stretch="UniformToFill"/> 
      </Button> 
     </RelativePanel> 

在接下來的網格行(Grid.Row =」 1」 )定義可滾動文本的可視區域。將該區域的可視性設置爲摺疊狀態,以禁止顯示測試,直到發生按鈕單擊事件。此網格行將包含另一個網格,其中兩行定義可滾動文本控件的Xaml以及一個關閉按鈕。第一個網格行將在一個ScrollableView中包含一個ListView,在我的情況下將包含在一個Border中。第二行包含關閉按鈕。

<Grid Grid.Row="1" 
        x:Name="ScrollableText"          
        Visibility="Collapsed" 
        Margin="10"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="*"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 

      <Border BorderThickness="2" 
        BorderBrush="Gray"> 
       <ScrollViewer ScrollViewer.VerticalScrollMode="Auto"> 
        <ListView ItemsSource="{x:Bind textList}" 
           HorizontalAlignment="Stretch" 
           HorizontalContentAlignment="Stretch" 
           ItemTemplate="{StaticResource TextBlockDataTemplate}" 
           Height="500"> 
         <ListView.ItemContainerStyle> 
          <Style TargetType="ListViewItem"> 
           <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
          </Style> 
         </ListView.ItemContainerStyle> 
        </ListView> 
       </ScrollViewer> 
      </Border> 

      <Button Grid.Row="1" 
        Name="CloseButton" 
        Content="Close" 
        Click="CloseButton_Click" 
        Margin="10"/> 
     </Grid> 

ItemTemplate中爲ListView在頁資源中定義顯示所述滾動文本的頁面上(對於更復雜的滾動文本,所述模板可以在用戶控件中定義),例如:

<DataTemplate x:Key="TextBlockDataTemplate" x:DataType="data:MyString"> 
      <TextBlock HorizontalAlignment="Left" 
          TextWrapping="Wrap" 
          FontSize="16" 
          FontFamily="Global Sans Serif" 
          Margin="10" 
          Height="Auto" 
         Text="{x:Bind MyParagraph}"/> 
     </DataTemplate> 

然後在每個按鈕的Click事件中,放置可滾動文本。該文本將包含一個類型爲單個字符串類的段落列表(在我的示例中爲MyString),它們被添加到爲ListView定義的ObservableCollection中。爲了使集合可以用於可滾動文本的多個視圖,在添加文本之前首先清除該集合。收集完成後,Grid.Row =「1」將變爲可見。

private void InformationButton_Click(object sender, RoutedEventArgs e) 
     { 
      textList.Clear(); 
      MyString line = new MyString(); 
      line.MyParagraph = "jMajorsIV provides golf score data management of 9 or 18 hole competition golf in a format that mimics play of the PGA tour major tournaments, namely The Masters, The US Open, the PGA, and The British Open. Each major tourney consists of four rounds and scoring is based on net score using a progressive handicap calculated weekly. Player standings at the end of each major are awarded Cup Points according to their finish. At the end of the four major tourneys, the players can decide on the cup points cut-off for participation in the Cup Championship."; 
      textList.Add(line); 
      MyString line1 = new MyString(); 
      line1.MyParagraph = "Competition can include at little as two players, personal groups, as well as organized leagues. Scoring can be entered at any time allowing play to conform to available play times and dates. Leader boards will show the latest round scored and the net score for that round while players not yet completing rounds will show dash marks for a current score. Over or under par total scores for each player for rounds completed will determine standings for the tourney."; 
      textList.Add(line1); 
      MyString line2 = new MyString(); 
      line2.MyParagraph = "Users are presented with a setup page to allow course data settings to be entered for the type of players competing(seniors, women, mixed, men's) and consists of tee slope and ratings, tee par per hole, tee hole handicap per hole, along with the course name or group name. The setup page provides for initial player entry but does not require all players to be entered; players an join as the competition ensues throughout the season. Play format is also selected for the competition and allows for settings the handicap percentage to be used for net scoring."; 
      textList.Add(line2); 
      MyString line3 = new MyString(); 
      line3.MyParagraph = "Scoring is entered by individual players or by a designee on consists of the gross score for each hole. Equitable score control is used based on the players handicap index to adjust gross score entries for net score determination. Handicap dots and over - under score are maintained as each hole score is entered. Leader score boards will automatically update as a score is entered. Player statistics are tabulated and displayed for each tournament round and for each hole on the course."; 
      textList.Add(line3); 
      MyString line4 = new MyString(); 
      line4.MyParagraph = " Comments and suggestions are welcome and can be sent to: [email protected] Product of jbhSolutions - Lake Monticello, VA"; 
      textList.Add(line4); 
      ScrollableText.Visibility = Visibility.Visible; 
     } 

關閉按鈕單擊事件摺疊的頁面上顯示的滾動視圖區域:

private void CloseButton_Click(object sender, RoutedEventArgs e) 
     { 
      ScrollableText.Visibility = Visibility.Collapsed; 
     } 

對於網絡和微軟應用程序商店這一控制動作遊「jbhSolutions」的例子。