Avalonia中如何将获焦TextBox背景设置为透明?
在Avalonia中实现获焦TextBox全透明背景(嵌套于模板化用户控件)
我需要在Avalonia中将处于获焦状态的TextBox背景改为完全透明,该TextBox位于模板化的自定义用户控件内,且嵌套在Border中。目前未获焦时TextBox能保持透明,但获焦后会出现非透明背景,问题与Fluent Theme相关,且无法移除该主题包(其他组件依赖)。
尝试过的代码及现象
初始样式代码
<Styles xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="using:AIVA.CustomComponents" > <Style Selector="TextBox:focus-within"> <Setter Property="Background" Value="{DynamicResource TransparentBrush}" /> <Setter Property="BorderBrush" Value="{DynamicResource TransparentBrush}" /> <Setter Property="BorderThickness" Value="0"></Setter> <Setter Property="Foreground" Value="Black" /> </Style> <Design.PreviewWith> <Border Background="Aquamarine" Padding="100"> <StackPanel> <controls:CustomInputField LabelText="ID" /> <controls:CustomInputField/> </StackPanel> </Border> </Design.PreviewWith> <Style Selector="controls|CustomInputField"> <Setter Property="Background" Value="Transparent"></Setter> <!-- Set Defaults --> <Setter Property="Template"> <ControlTemplate> <Border Width="350" Height="25" Margin="0,25,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="0,0,0,2"> <StackPanel Orientation="Horizontal"> <TextBlock Width="100" Height="26" VerticalAlignment="Center" FontFamily="Montserrat" FontSize="18" FontWeight="Medium" Text="{TemplateBinding LabelText}" TextAlignment="Left" /> <TextBox x:Name="TextInput" Width="250" Height="26" VerticalContentAlignment="Center" Background="{DynamicResource TransparentBrush}" BorderThickness="0,0,0,0" FontSize="18" FontWeight="Medium" TextAlignment="Center" Text="{TemplateBinding InputText}" > </TextBox> </StackPanel> </Border> </ControlTemplate> </Setter> </Style> </Styles>
现象:仅未获焦的TextBox保持透明背景
带类选择器的样式代码
<Style Selector="TextBox.CustomInput"> <Setter Property="Background" Value="{DynamicResource TransparentBrush}"></Setter> <Setter Property="BorderBrush" Value="{DynamicResource TransparentBrush}"></Setter> </Style> <Style Selector="TextBox.CustomInput:focus-within"> <Setter Property="Background" Value="{DynamicResource TransparentBrush}"></Setter> <Setter Property="BorderBrush" Value="{DynamicResource TransparentBrush}"></Setter> </Style>
现象:获焦后仍未实现透明
解决方案
由于Fluent Theme的TextBox模板在获焦时会通过内部资源或视觉状态触发器覆盖自定义样式,可通过以下几种方式解决:
方案1:使用高优先级选择器覆盖样式
针对自定义控件内的TextBox,使用更具体的选择器强制覆盖Fluent Theme的默认样式:
<Style Selector="controls|CustomInputField TextBox:focus-within"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="BorderThickness" Value="0" /> <!-- 重写TextBox模板,移除内部默认背景 --> <Setter Property="Template"> <ControlTemplate TargetType="TextBox"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <ScrollViewer x:Name="PART_ContentHost" Padding="{TemplateBinding Padding}" Background="Transparent" /> </Border> </ControlTemplate> </Setter> </Style>
方案2:重写TextBox视觉状态
保留TextBox默认行为,仅修改获焦状态的背景值:
<Style Selector="TextBox:focus-within"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="Transparent" /> <!-- 强制覆盖视觉状态的背景设置 --> <Setter Property="VisualStateManager.VisualStateGroups"> <VisualStateGroupList> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <VisualState x:Name="PointerOver" /> <VisualState x:Name="Focused"> <VisualState.Setters> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="Transparent" /> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateGroupList> </Setter> </Style>
方案3:覆盖Fluent Theme默认资源
直接替换主题中TextBox获焦时使用的背景资源,全局生效:
<Application.Resources> <SolidColorBrush x:Key="TextBoxFocusedBackground" Color="Transparent" /> <SolidColorBrush x:Key="TextBoxFocusedBorderBrush" Color="Transparent" /> </Application.Resources>
内容的提问来源于stack exchange,提问作者Alexandro Cardona




