.NET MAUI Windows平台:如何保持Entry hover/点击时的自定义边框
MAUI Windows Entry 保持hover/焦点状态下的自定义红色边框
问题场景
在MAUI中通过自定义EntryHandler设置Entry的边框粗细为1、边框颜色为红色时,正常状态显示正常,但鼠标hover或点击获取焦点后,边框会恢复默认样式。尝试过绑定焦点事件、全局设置TextControlBorderThemeThickness均无效。
解决思路
Windows平台下MAUI的Entry对应原生TextBox控件,其hover、焦点等状态的样式由VisualStateManager管理,直接设置BorderBrush和BorderThickness会被视觉状态的触发器覆盖。需要修改视觉状态中的边框相关设置,或者替换默认样式。
方法一:在EntryHandler中修改VisualState
直接在自定义Handler映射中,找到TextBox的视觉状态组,覆盖PointerOver(hover)和Focused状态下的边框属性:
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("CustomBorder", (handler, view) => { if (view is Entry entry) { #if WINDOWS var textBox = handler.PlatformView; // 设置默认状态边框 textBox.BorderThickness = new Microsoft.UI.Xaml.Thickness(1); var redBrush = new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.Maui.Graphics.Colors.Red.ToWindowsColor()); textBox.BorderBrush = redBrush; // 遍历视觉状态组,修改hover和焦点状态 var visualStateGroups = Microsoft.UI.Xaml.VisualStateManager.GetVisualStateGroups(textBox); foreach (var group in visualStateGroups) { if (group is Microsoft.UI.Xaml.VisualStateGroup stateGroup && stateGroup.Name == "CommonStates") { foreach (var state in stateGroup.States) { switch (state.Name) { case "PointerOver": case "Focused": state.Setters.Clear(); state.Setters.Add(new Microsoft.UI.Xaml.Setter { Property = Microsoft.UI.Xaml.Controls.TextBox.BorderBrushProperty, Value = redBrush }); state.Setters.Add(new Microsoft.UI.Xaml.Setter { Property = Microsoft.UI.Xaml.Controls.TextBox.BorderThicknessProperty, Value = new Microsoft.UI.Xaml.Thickness(1) }); break; } } } } #endif } });
方法二:自定义TextBox样式并应用
通过自定义完整的TextBox模板,完全控制所有状态下的样式:
- 在Windows项目的
App.xaml中添加自定义样式:
<maui:MauiWinUIApplication.Resources> <Style TargetType="TextBox" x:Key="CustomEntryStyle"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Red"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TextBox"> <Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="PointerOver"> <VisualState.Setters> <Setter Property="BorderBrush" Value="Red"/> <Setter Property="BorderThickness" Value="1"/> </VisualState.Setters> </VisualState> <VisualState x:Name="Focused"> <VisualState.Setters> <Setter Property="BorderBrush" Value="Red"/> <Setter Property="BorderThickness" Value="1"/> </VisualState.Setters> </VisualState> <VisualState x:Name="Disabled"> <VisualState.Setters> <Setter Property="BorderBrush" Value="Gray"/> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Border x:Name="BorderElement" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> <ContentPresenter x:Name="ContentPresenter" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Text}" ContentTemplate="{TemplateBinding ContentTemplate}"/> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </maui:MauiWinUIApplication.Resources>
- 在EntryHandler中应用该样式:
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("CustomBorder", (handler, view) => { if (view is Entry) { #if WINDOWS var textBox = handler.PlatformView; textBox.Style = (Microsoft.UI.Xaml.Style)textBox.Resources["CustomEntryStyle"]; #endif } });
说明
- 方法一无需重新定义控件模板,仅修改目标状态的属性,适合快速调整;
- 方法二通过自定义模板实现完全样式控制,可按需扩展其他视觉效果(如背景色、字体样式等)。
内容的提问来源于stack exchange,提问作者cera36
相关产品推荐
相关产品推荐

