开发者

WPF: getting AccessText in ScrollViewer to wrap

开发者 https://www.devze.com 2023-02-03 00:23 出处:网络
I have a Grid containing a ScrollViewer containing AccessText.I want the AccessText to take up the full width of the ScrollViewer, which should take up the full width of the Grid, without any overflow

I have a Grid containing a ScrollViewer containing AccessText. I want the AccessText to take up the full width of the ScrollViewer, which should take up the full width of the Grid, without any overflow. Currently, the contents of my AccessText are cut off on the right side of the screen instead of wrapp开发者_运维问答ing. I have tried setting AccessText.TextWrapping to Wrap, WrapWithOverflow, and I've also tried removing the property entirely. I switched to using a Grid instead of a StackPanel because I thought that might affect how the contents are sized, but that hasn't helped. Here's what I have:

<Grid MaxHeight="40">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Whee a label:" Grid.Column="0"/>
    <ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto"
                  HorizontalScrollBarVisibility="Auto">
        <AccessText Text="{Binding MyLongTextField}"/>
    </ScrollViewer>
</Grid>


When you set ColumnDefinition Width to Auto, the ScrollViewer within it won't be limited by the "visible Width" of the Column, so it will still take up as much horizontal space as it needs. With the xaml you posted, I think Width="*" will suit your needs. For the ScrollViewer, it seems like you don't want it to be able to Scroll horizontaly but only verticaly? In that case, set HorizontalScrollBarVisibility="Disabled". Otherwise you'll get a Horizontal ScrollBar.

<Grid MaxHeight="40">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Whee a label:" Grid.Column="0"/>
    <ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto"
                  HorizontalScrollBarVisibility="Disabled">
        <AccessText TextWrapping="Wrap" Text="{Binding MyLongTextField}"/>
    </ScrollViewer>
</Grid>


If you simply want the AccessText to wrap indefinitely, modify your second ColumnDefinition from Auto to * and move the AccessText outside of the ScrollViewer as seen below...

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
             <TextBlock Text="Whee a label:" Grid.Column="0"/>
             <AccessText Grid.Column="1" TextWrapping="Wrap" Text="{Binding MyLongTextField}"/>
    </Grid>

The reason the text would not wrap is because the second ColumnDefinition was set to Auto; which essentially does not force a bounds around the AccessText.

If you want to keep the ScrollViewer try this...

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Whee a label:" Grid.Column="0"/>
    <ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto">
        <AccessText  TextWrapping="Wrap" Text="{Binding MyLongTextField}"/>
    </ScrollViewer>
</Grid>


This ended up giving me what I wanted:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Whee a label:" Grid.Column="0"/>
    <ScrollViewer Grid.Column="1" MaxHeight="40"
                  HorizontalScrollBarVisibility="Disabled"
                  VerticalScrollBarVisibility="Auto">
        <AccessText Text="{Binding CRData.Error}" TextWrapping="Wrap"/>
    </ScrollViewer>
</Grid>

Thanks to Meleak and Aaron for the suggestion of using * for the column width instead of Auto, and to Meleak for suggesting Disabled for the horizontal scrollbar instead of Auto.

0

精彩评论

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