【问题标题】:Displaying Border on TextBlock when TextBlock placed inside ScrollViewer当 TextBlock 放置在 ScrollViewer 中时在 TextBlock 上显示边框
【发布时间】:2012-09-16 06:19:30
【问题描述】:

我有 WPF 应用程序,在我的 WPF 表单之一中有 TextBlock。我在 ScrollViewer 中放置了 TextBlock 以提供滚动功能。我想要 TextBlock 上的边框,所以我在XAML 中编写了以下代码。

<ScrollViewer Margin="230,32,12,147">
    <Border BorderThickness="1" BorderBrush="Black">
          <TextBlock Name="textBlock1"></TextBlock>
      </Border>
 </ScrollViewer>

滚动条 GUI 显示如下。

向下滚动到 TextBlock 时显示底部边框。用户体验不适合这种设计。所以,我尝试了下面的代码,但没有显示边框。

<Border BorderThickness="1" BorderBrush="Black">
    <ScrollViewer Margin="230,32,12,147">
          <TextBlock Name="textBlock1"></TextBlock>
     </ScrollViewer>
 </Border>

TextBlock 放置在 ScrollViewer 中时如何显示边框?

【问题讨论】:

  • 我解决了

标签: c# wpf user-interface


【解决方案1】:

在 ScrollViewer 控件中设置边框!

<ScrollViewer Margin="230,32,12,147" BorderThickness="5">
      <TextBlock Name="textBlock1"></TextBlock>
 </ScrollViewer>

在属性窗口中,展开Other组并设置BorderThickness

下一个代码运行良好:

<ScrollViewer Height="116"  Margin="115,112,0,0" Width="269">
        <Border BorderBrush="Silver" BorderThickness="5" Height="100" Width="200">
            <TextBlock Height="69" Name="textBlock1" Text="TextBlock" />
        </Border>
    </ScrollViewer>

外边框代码示例:

<Border BorderBrush="Silver" BorderThickness="5" Height="100" HorizontalAlignment="Left" Margin="167,104,0,0" Name="border1" VerticalAlignment="Top" Width="200">
        <ScrollViewer Height="83" Name="sv" Width="184">
            <TextBlock Name="textBlock1" Text="TextBlock TextBlockTextBlockTextBlock TextBlock TextBlock TextBlock TextBlock TextBlockTextBlockTextBlock TextBlock TextBlock TextBloc TextBlock TextBlockTextBlockTextBlock TextBlock TextBlock TextBloc TextBlock TextBlockTextBlockTextBlock TextBlock TextBlock TextBloc" TextWrapping="Wrap" />
        </ScrollViewer>
    </Border>

【讨论】:

  • 你设置BorderBrush颜色了吗?
  • 是的,设置 BorderBrush="Black" 后不显示边框
  • 好的,ScrollViewer 的边框在 Silverlight 中有效,但在 WPF 中无效。看看我的回答,已经编辑过了。
  • 在您的解决方案中,如果我指定了 Border 的 Height 和 width 属性,那么如果 TextBlock 中有 100 行文本,则不会显示滚动条。作为我的问题,我希望 ScrollBar 和 Border 在 Scrollviewer 之外,所以用户体验很好。你可以看到我在我的问题中实现了滚动条和边框(见图)。但是用户体验有问题。我想在滚动查看器之外显示边框。
  • 是的,你说得对,它在 Silverlight 中工作,但我希望在 WPF 中使用它。我关于 WPF 的问题。
猜你喜欢
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多