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

如何在.NET MAUI中重写Windows平台TextBox默认样式并移除清除按钮

解决Windows平台自定义Entry控件的样式覆盖与Clear按钮移除问题

一、重写TextBox默认样式(覆盖悬停/聚焦效果)

Windows平台的TextBox默认视觉状态(如悬停背景变化、聚焦底部边框)由内置Style定义,仅通过代码设置基础属性无法覆盖这些状态行为,需要自定义完整的控件模板来替换默认样式:

  1. 在MAUI Windows项目的App.xaml(或新建ResourceDictionary并合并到App.xaml)中添加自定义TextBox样式:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="CustomTextBoxStyle" TargetType="TextBox">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="CornerRadius" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Grid>
                        <Border x:Name="BorderElement"
                                Background="{TemplateBinding Background}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                CornerRadius="{TemplateBinding CornerRadius}">
                            <ScrollViewer x:Name="ContentElement"
                                          Padding="{TemplateBinding Padding}"
                                          HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                                          HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                          VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                                          VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                          IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                                          IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                                          ZoomMode="Disabled"
                                          AutomationProperties.AccessibilityView="Raw"/>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <!-- 覆盖悬停状态,取消背景变化 -->
                        <Trigger Property="IsPointerOver" Value="True">
                            <Setter TargetName="BorderElement" Property="Background" Value="Transparent"/>
                        </Trigger>
                        <!-- 覆盖聚焦状态,取消边框变化 -->
                        <Trigger Property="IsFocused" Value="True">
                            <Setter TargetName="BorderElement" Property="BorderBrush" Value="Transparent"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
  1. 在CustomEntryHandler的CreatePlatformView方法中应用自定义样式:
protected override TextBox CreatePlatformView()
{
    var nativeView = new TextBox();

    // 应用自定义样式
    nativeView.Style = (Microsoft.UI.Xaml.Style)Microsoft.UI.Xaml.Application.Current.Resources["CustomTextBoxStyle"];
    
    nativeView.Margin = new Microsoft.UI.Xaml.Thickness(0);
    nativeView.FocusVisualMargin = new Microsoft.UI.Xaml.Thickness(0);
    nativeView.ClearButtonVisibility = Microsoft.UI.Xaml.Controls.ClearButtonVisibility.Collapsed;

    return nativeView;
}

二、移除TextBox的Clear按钮

直接设置TextBox的ClearButtonVisibility属性为Collapsed即可,上述代码已包含该设置,单独实现只需添加:

nativeView.ClearButtonVisibility = Microsoft.UI.Xaml.Controls.ClearButtonVisibility.Collapsed;

说明

自定义ControlTemplate是彻底覆盖TextBox默认视觉行为的核心,通过这种方式可以完全消除默认的悬停、聚焦等状态效果,实现完全自定义的视觉表现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 01:01:06