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

WPF TextBox设计器可见但应用运行时不可见的问题求助

WPF TextBox设计器可见但运行时消失

我是WPF新手,可能遗漏了一些明显细节。我的TextBox在Designer中显示正常且可见,但运行应用程序时该TextBox不可见,而其他元素仍正常显示。

视图对比

  • 设计器视图:设计器视图
  • 运行时视图:应用运行视图

对应XAML代码

<Window x:Class="Windows.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Windows"
        mc:Ignorable="d"
        Title="UniPaste for Windows" Height="450" Width="800"
        Background="#333">
    <Grid>
        <TextBlock TextWrapping="Wrap" Text="UniPaste for Windows" Margin="0,0,0,348" TextAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="32" FontFamily="Consolas Bold"/>
        <TextBlock TextWrapping="Wrap" Text="Email" Margin="0,90,0,0" TextAlignment="Center" VerticalAlignment="Top" Foreground="White" FontSize="24" FontFamily="Consolas"/>
        <TextBox Name="Email" Margin="340,123,340,290" Background="White" Height="23" TextChanged="EmailChanged"/>
    </Grid>
</Window>

解决方案

问题出在TextBox的Margin属性设置上。WPF中Margin的顺序是左、上、右、下,你给TextBox设置的Margin="340,123,340,290",再加上固定Height="23",窗口运行时的客户区高度会比设计时设置的Height="450"小(因为要扣除标题栏高度),导致TextBox被下边距挤到了可视区域之外。

修复方法1:调整Margin数值

减少下边距的数值,同时可以去掉固定Height让控件自适应:

<TextBox Name="Email" Margin="340,123,340,280" Background="White" TextChanged="EmailChanged"/>

修复方法2:使用Grid行定义布局(推荐)

依赖固定Margin的布局很容易出现适配问题,改用Grid的行定义来分配空间,布局会更稳定:

<Window x:Class="Windows.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Windows"
        mc:Ignorable="d"
        Title="UniPaste for Windows" Height="450" Width="800"
        Background="#333">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/> <!-- 顶部标题占剩余空间 -->
            <RowDefinition Height="Auto"/> <!-- Email标签自适应高度 -->
            <RowDefinition Height="Auto"/> <!-- TextBox自适应高度 -->
            <RowDefinition Height="*"/> <!-- 底部空白占剩余空间 -->
        </Grid.RowDefinitions>
        
        <TextBlock Grid.Row="0" TextWrapping="Wrap" Text="UniPaste for Windows" 
                   TextAlignment="Center" VerticalAlignment="Center" 
                   Foreground="White" FontSize="32" FontFamily="Consolas Bold"/>
                   
        <TextBlock Grid.Row="1" TextWrapping="Wrap" Text="Email" 
                   TextAlignment="Center" Margin="0,20,0,10"
                   Foreground="White" FontSize="24" FontFamily="Consolas"/>
                   
        <TextBox Grid.Row="2" Name="Email" Margin="340,0,340,0" 
                 Background="White" TextChanged="EmailChanged"/>
    </Grid>
</Window>

这种方式下,无论窗口大小怎么调整,控件都会保持正确的位置,不会出现被挤出的情况。


内容的提问来源于stack exchange,提问作者H. Ross

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 18:37:14