You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ViewBox内TextBox显示异常求助:左对齐失效+字体过大

问题分析与修复方案

问题原因

  • 字体尺寸过大:ViewBox的Stretch="Fill"属性会对内部所有元素进行等比拉伸,包括你设置的FontSize="20",导致字体被强制放大,超出常规文本框的尺寸。
  • TextBox无法左对齐:未给TextBox设置明确的宽度约束,ViewBox在拉伸时会基于TextBox的默认内容尺寸进行缩放,破坏了原本的左对齐布局逻辑。

修复方案

不需要用ViewBox来实现TextBox宽度与窗口一致的效果,直接移除包裹TextBox的ViewBox,并给TextBox添加HorizontalAlignment="Stretch"属性,让它自动填充所在的Grid行宽度,同时保留你设置的字体大小。

修改后的关键代码片段

<Grid Grid.Row="1">
    <!-- 移除ViewBox,直接放置TextBox -->
    <TextBox 
        FontSize="20" 
        Foreground="Black" 
        TextAlignment="Left" 
        BorderThickness="0" 
        Drop="myText_Drop_1" 
        DragOver="myText_DragOver_1" 
        AllowDrop="True" 
        CanDrag="True" 
        TextChanged="myText_TextChanged" 
        TextWrapping="Wrap" 
        AcceptsReturn="True" 
        Name="myText"
        HorizontalAlignment="Stretch"  <!-- 添加此属性让TextBox占满宽度 -->
        Margin="0"  <!-- 确保没有额外边距影响对齐 -->
    ></TextBox>
</Grid>

完整修改后的XAML代码

<Page
    x:Class="Labb3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Labb3"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock Foreground="Black" Name="Title" Text="Namlöst dokument" FontSize="22"></TextBlock>
            <CommandBar Background="LightBlue" Grid.Row="1" HorizontalAlignment="Left" OverflowButtonVisibility="Collapsed">
                <AppBarButton Click="AppBarButton_Click" Name="NewFileButton" Icon="Add" Label="New File.."></AppBarButton>
                <AppBarButton Click="AppBarButton_Click" Name="OpenFileButton" Icon="OpenFile" Label="Open File.."></AppBarButton>
                <AppBarButton Click="AppBarButton_Click" Name="SaveFileButton" Icon="Save" Label="Save File.."></AppBarButton>
                <AppBarButton Click="AppBarButton_Click" Name="SaveLocalButton" Icon="SaveLocal" Label="Spara Som..."></AppBarButton>
                <AppBarButton Click="AppBarButton_Click" Name="ClearButton" Icon="Clear" Label="Clear..."></AppBarButton>
            </CommandBar>
        </Grid>
        <!-- 替换原ViewBox为Grid,直接放置TextBox -->
        <Grid Grid.Row="1">
            <!--Text Wrapping="Wrap" och AcceptsReturn="True" gör att man kan skriva flera rader i rutan-->
            <TextBox 
                FontSize="20" 
                Foreground="Black" 
                TextAlignment="Left" 
                BorderThickness="0" 
                Drop="myText_Drop_1" 
                DragOver="myText_DragOver_1" 
                AllowDrop="True" 
                CanDrag="True" 
                TextChanged="myText_TextChanged" 
                TextWrapping="Wrap" 
                AcceptsReturn="True" 
                Name="myText"
                HorizontalAlignment="Stretch"
                Margin="0"
            ></TextBox>
        </Grid>
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <!--原底部TextBlock的ViewBox保留,若不需要缩放可同理移除-->
            <Viewbox Stretch="Fill">
                <TextBlock Margin="10,10,20,10" Foreground="Black" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name="Antaltecken" Text="Antal tecken: "></TextBlock>
            </Viewbox>
            <Viewbox Stretch="Fill" Grid.Column="1">
                <TextBlock Margin="10,10,20,10" Foreground="Black" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name="Antalteckenmellanslag" Grid.Column="1" Text="Antal tecken med mellanslag: "></TextBlock>
            </Viewbox>
            <Viewbox Stretch="Fill" Grid.Column="2">
                <TextBlock Margin="10,10,20,10" Foreground="Black" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name="Antalord" Grid.Column="2" Text="Antal ord: "></TextBlock>
            </Viewbox>
            <Viewbox Stretch="Fill" Grid.Column="3">
                <TextBlock Margin="10,10,20,10" Foreground="Black" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name="Antalrader" Grid.Column="3" Text="Antal rader: "></TextBlock>
            </Viewbox>
        </Grid>
    </Grid>
</Page>

效果说明

  • TextBox会自动填充所在Grid行的宽度,与窗口宽度保持一致。
  • 字体大小保持FontSize="20"的常规尺寸,不会被缩放。
  • TextAlignment="Left"可以正常生效,文本左对齐显示。

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 11:27:33