1

我正在尝试使TextBox文本换行并与Window. 如果我不设置ScrollViewer'HorizontalScrollBarVisibility属性,那将有效。但如果我这样做,TextBox将无限增长。该问题通过将MaxWidth属性设置为 TextBox 来解决,但在这种情况下TextBox不会超出该MaxWidth值。然后我的想法是绑定MaxWidthColumnDefinition's ActualWidth,但这也不起作用,因为ColumnDefinition'sActualWidth属性不是DependencyProperty. 这是我的代码:

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Text="Name:" Margin="5" />
            <TextBox MinWidth="200" Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" AcceptsReturn="True" Margin="5"/>
        </Grid>
    </ScrollViewer>

任何想法将不胜感激。

4

1 回答 1

2

尝试这个:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <TextBlock Text="Name:" Margin="5" />
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Column="1">
        <TextBox MinWidth="200" TextWrapping="Wrap" AcceptsReturn="True" Margin="5" MaxWidth="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ScrollViewer}}}" />
    </ScrollViewer>
</Grid>
于 2012-09-14T06:57:42.517 回答