C#WPF - 的ScrollViewer + TextBlock的烦恼烦恼、WPF、ScrollViewer、TextBlock

2023-09-03 02:22:57 作者:余温散尽

我有一个的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>
< /窗>
 
WPF 二

解决方案

本作品:

 <窗​​口的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>