C#WPF - 的ScrollViewer + TextBlock的烦恼 [英] C# WPF - ScrollViewer + TextBlock troubles

查看:752
本文介绍了C#WPF - 的ScrollViewer + TextBlock的烦恼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的TextBlock 的ScrollViewer 与舒展它的窗口对齐。我需要的的TextBlock 表现为以下几点:

  • 在大小调整与窗口,没有滚动条
  • 当调整到一定宽度的TextBlock 需要保持一个了minWidth 和滚动条应该会出现
  • TextWrapping 的TextTrimming 应该适当地

我怎样才能得到这个功能?

我曾尝试多种方法,包括绑定 ActualWidth的&放大器; 的ActualHeight ,但不能让它的工作。

这不能那么难,我缺少什么?

下面是一个code样品投入的XamlPad(无时了minWidth尚未设置):

 <窗​​口的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation的xmlns:X =HTTP://模式。 microsoft.com/winfx/2006/xaml">
    <的ScrollViewer Horizo​​ntalScrollBarVisibility =自动VerticalScrollBarVisibility =自动>
            < TextBlock的TextWrapping =自动换行文本=一些很长的文字,当你调整窗口或许应该换行 />
    < /的ScrollViewer>
< /窗>
 

解决方案

本作品:

 <窗​​口的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation的xmlns:X =HTTP://模式。 microsoft.com/winfx/2006/xaml">
    <的ScrollViewer Horizo​​ntalScrollBarVisibility =自动VerticalScrollBarVisibility =自动NAME =滚轮>
            < TextBlock中的Horizo​​ntalAlignment =拉伸VerticalAlignment =拉伸了minWidth =100宽度={结合的ElementName =滚轮,路径= ViewportWidth}
            TextWrapping =自动换行文本=一些,当你调整窗口或许应该换行很长的文字。 />
    < /的ScrollViewer>
< /窗>
 

I have a TextBlock within a ScrollViewer that aligns with stretch to its window. I need the TextBlock to behave as the following:

  • Resizes with window, no scrollbars
  • When resized below a certain width the TextBlock needs to keep a MinWidth and scrollbars should appear
  • TextWrapping or TextTrimming should work appropriately

How can I get this functionality?

I have tried several ways, involving bindings to ActualWidth & ActualHeight, but can't get it to work.

This can't be that difficult, what am I missing?

Here is a code sample to put in XamlPad (no MinWidth is set yet):

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <TextBlock TextWrapping="Wrap" Text="Some really long text that should probably wordwrap when you resize the window." />
    </ScrollViewer>
</Window>

解决方案

This works:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Name="Scroller">
            <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="100" Width="{Binding ElementName=Scroller, Path=ViewportWidth}"
            TextWrapping="Wrap" Text="Some really long text that should probably wordwrap when you resize the window." />
    </ScrollViewer>
</Window>

这篇关于C#WPF - 的ScrollViewer + TextBlock的烦恼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆