如何获取TextBlock将文本包装在DockPanel区域内?

时间:2010-03-31 11:18:00

标签: silverlight textblock textwrapping dockpanel

如何让内部TextBlock在不定义绝对宽度的情况下包装文本,我该怎么办?

我尝试过Width = Auto,Stretch,TextWrapping,把它放在StackPanel中,似乎没什么用。

alt text http://www.deviantsart.com/upload/120rjpv.png

XAML:

<UserControl x:Class="Test5.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    Width="800"
    Height="600">
    <tk:DockPanel LastChildFill="True">

        <StackPanel tk:DockPanel.Dock="Top"
            Width="Auto"
            Height="50"
            Background="#eee">
            <TextBlock Text="{Binding TopContent}"/>
        </StackPanel>

        <StackPanel tk:DockPanel.Dock="Bottom" Background="#bbb"
            Width="Auto"
            Height="50">
            <TextBlock Text="bottom area"/>
        </StackPanel>

        <StackPanel tk:DockPanel.Dock="Right" Background="#ccc"
            Width="200"
            Height="Auto">
            <TextBlock Text="info panel"/>
        </StackPanel>

        <StackPanel tk:DockPanel.Dock="Left" Background="#ddd"
            Width="Auto"
            Height="Auto">
            <ScrollViewer HorizontalScrollBarVisibility="Auto" Padding="10"
            BorderThickness="0"
                Width="Auto"
                VerticalScrollBarVisibility="Auto">
                <tk:DockPanel HorizontalAlignment="Left" Width="Auto" >
                    <StackPanel tk:DockPanel.Dock="Top" HorizontalAlignment="Left">
                        <Button Content="Add More" Click="Button_Click"/> 
                    </StackPanel>
                    <TextBlock tk:DockPanel.Dock="Top" 
                        Text="{Binding MainContent}" 
                        Width="Auto" 
                        TextWrapping="Wrap" />
                </tk:DockPanel>
            </ScrollViewer>
        </StackPanel>

    </tk:DockPanel>
</UserControl>

2 个答案:

答案 0 :(得分:1)

您是否尝试更改滚动条可见性?

<ScrollViewer HorizontalScrollBarVisibility="Disabled" Padding="10"
            BorderThickness="0"
                Width="Auto"
                VerticalScrollBarVisibility="Auto">

答案 1 :(得分:1)

出于某种原因,明显(优雅)的解决方案在SL中似乎不起作用:

<ScrollViewer Name="w_scrollViewer" HorizontalScrollBarVisibility="Auto" Padding="10"
BorderThickness="0" Width="Auto" VerticalScrollBarVisibility="Auto">
    <tk:DockPanel Name="w_dp" HorizontalAlignment="Left" Width="Auto" >
        <TextBlock Margin="2" Name="w_tb"
                   Text="{Binding MainContent}" TextWrapping="Wrap"
                   Width="{Binding ActualWidth, ElementName=w_scrollViewer}"
                    />
    </tk:DockPanel>
</ScrollViewer>

但有点“粗糙”的方式:

<StackPanel SizeChanged="w_sp_SizeChanged" Name="w_sp" tk:DockPanel.Dock="Left"
            Background="#ddd" Width="Auto" Height="Auto">
    <ScrollViewer Name="w_scrollViewer" HorizontalScrollBarVisibility="Auto"
                  Padding="10" Width="Auto" VerticalScrollBarVisibility="Auto"
                  SizeChanged="HandleScrollViewerSizeChanged" >
        <tk:DockPanel HorizontalAlignment="Left" Width="Auto" >
            <TextBlock Name="w_textBlock" Text="{Binding MainContent}" TextWrapping="Wrap" />
        </tk:DockPanel>
    </ScrollViewer>
</StackPanel>
代码背后的代码:

private void HandleScrollViewerSizeChanged(object sender, SizeChangedEventArgs e)
{
    w_textBlock.Width = w_scrollViewer.ActualWidth;
}