WPF _ TextBlock Vertical ScrollBar问题

时间:2015-04-23 09:12:59

标签: c# wpf textblock vertical-scrolling

我有一个堆栈面板,如下所示。

    <Stackpanel>
         <Grid>
             <Grid.RowDefinitions>
                 <RowDefinition/>
                 <RowDefinition/>
             </Grid.RowDefinitions>

         < TextBlock Text="SomeText"  Grid.Row="0"/>

         <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" >

             <TextBlock Text="Some Other Text"/>
       </ScrollViewer>
    </Grid>
    </StackPanel>

我希望第二个文本块有一个垂直滚动条。但它没有显示垂直滚动条。有谁知道这是什么问题?

3 个答案:

答案 0 :(得分:2)

这个工作试试这个.cs页面添加这一行txt1.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
         </Grid.ColumnDefinitions>
    <ScrollViewer>
        <StackPanel x:Name="RightPanel"
                    Margin="5,0"
                    ScrollViewer.HorizontalScrollBarVisibility="Visible"
                    ScrollViewer.VerticalScrollBarVisibility="Visible">
            <TextBox TextWrapping="Wrap" Height="50" x:Name="txt1"  Text="Some Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other Text"/>
        </StackPanel>
    </ScrollViewer>
</Grid>

答案 1 :(得分:0)

尝试这样的事情(在TextBox中设置ScrollViewer Width and Height和TextWrapping):

<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Visible" 
              HorizontalScrollBarVisibility="Disabled" 
              Width="70" Height="50">
     <TextBlock Text="Some Other Text tststssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" 
                TextWrapping="Wrap"/>
 </ScrollViewer>

答案 2 :(得分:0)

  1. 摆脱stackpanel
  2. Height添加到RowDefinition s
  3. TextWrapping
  4. 设置TextBlock

    所以你的代码应该是这样的:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    
        <TextBlock Text="SomeText"  Grid.Row="0"/>
    
        <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" >
            <TextBlock TextWrapping="Wrap" Text="Some Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other TextSome Other Text"/>
        </ScrollViewer>
    </Grid>