开发者

How to get a TextBlock to wrap text inside a DockPanel area?

开发者 https://www.devze.com 2022-12-25 01:01 出处:网络
What do I have to do to get the inner TextBlock below to wrap its text without defining an absolute width?

What do I have to do to get the inner TextBlock below to wrap its text without defining an absolute width?

I've tried Width=Auto, Stretch, TextWrapping, putting it in a StackPanel, nothing seems to work.

How to get a TextBlock to wrap text inside a DockPanel area?

(source: deviantsart.com)

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="开发者_如何学GoTrue">

        <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>


Have you tried changing the scrollbar visibility?

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


For some reason the obvious (elegant) solution doesn't seem to work in 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>

but a bit more "rough" way:

<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>

code behind:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号