每当文本框被聚焦时,文本的边框和前景都会根据当前主题进行更改:
主题灯 - >边框:黑色,文字:白色,背景:透明
主题黑暗 - >边框:白色,文字:白色,背景:白色
我希望文本框始终有边框:白色,文字:白色,背景:透明
当文本框没有聚焦时,我可以控制这些设置,但是当它有焦点时我无法改变背景。
文本框的XML是:
<TextBox InputScope="EmailSmtpAddress" Height="72" LostFocus="textBox1_LostFocus" GotFocus="textBox1_GotFocus" HorizontalAlignment="Left" Margin="0,62,0,0" Name="textBox1" Text="Enter URL here..." VerticalAlignment="Top" Width="460" TextChanged="textBox1_TextChanged" Foreground="White" Background="#005DADF5" SelectionForeground="White" BorderBrush="#DEEAEAEA" FontFamily="Tahoma" FontWeight="Normal" />
答案 0 :(得分:10)
您可以使用模板覆盖默认样式:
<Style x:Key="TextBoxStyle1" TargetType="TextBox">
<Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}" />
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}" />
<Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}" />
<Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}" />
<Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}" />
<Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}" />
<Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}" />
<Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}" />
<Setter Property="Padding" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBorder" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyBorder" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="ReadOnly">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBorder" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyBorder" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyBorder" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyBorder" Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyContent" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBorder" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}" />
</ObjectAnimationUsingKeyFrames>
<!-- COMMENT THIS OUT -----> <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBorder" Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBorderBrush}" />
</ObjectAnimationUsingKeyFrames>-->
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="EnabledBorder"
Margin="{StaticResource PhoneTouchTargetOverhang}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentControl x:Name="ContentElement"
Margin="{StaticResource PhoneTextBoxInnerMargin}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
BorderThickness="0"
Padding="{TemplateBinding Padding}" />
</Border>
<Border x:Name="DisabledOrReadonlyBorder"
Margin="{StaticResource PhoneTouchTargetOverhang}"
Background="Transparent"
BorderBrush="{StaticResource PhoneDisabledBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Visibility="Collapsed">
<TextBox x:Name="DisabledOrReadonlyContent"
Background="Transparent"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}"
Foreground="{StaticResource PhoneDisabledBrush}"
IsReadOnly="True"
SelectionBackground="{TemplateBinding SelectionBackground}"
SelectionForeground="{TemplateBinding SelectionForeground}"
Template="{StaticResource PhoneDisabledTextBoxTemplate}"
Text="{TemplateBinding Text}"
TextAlignment="{TemplateBinding TextAlignment}"
TextWrapping="{TemplateBinding TextWrapping}" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我使用Expression Blend获取默认模板,然后通过删除(注释掉)ObjectAnimationUsingKeyFrames
元素来修改它,该元素更改了BorderBrush
Focused
中的VisualState
。将此样式应用于TextBox
并将其BorderBrush
更改为您的颜色。
<TextBox BorderBrush="Red" Style="{StaticResource TextBoxStyle1}" />
答案 1 :(得分:9)
最简单的方法是在GotFocus上设置事件。
TextBox的XamlCode:
<TextBox x:Name="InputUserName"
Foreground="Black"
Background="LightGray"
BorderBrush="LightGray"
GotFocus="GotFocusaAction" />
GotFocusAction的Codebehind:
private void GotFocusaAction(object sender, System.Windows.RoutedEventArgs e)
{
InputUserName.Background = new SolidColorBrush(Colors.LightGray);
InputUserName.BorderBrush = new SolidColorBrush(Colors.LightGray);
}
如果要将聚焦TextBox的颜色设置为其他颜色,还应设置LostFocus的事件以更改BackgroundColor。
TextBox的XamlCode:
<TextBox x:Name="InputUserName"
Foreground="Black"
Background="Red"
BorderBrush="Red"
GotFocus="GotFocusaAction"
LostFocus="GotFocusaAction" />
Codebehind:
private void GotFocusaAction(object sender, System.Windows.RoutedEventArgs e)
{
InputUserName.Background = new SolidColorBrush(Colors.Purple);
InputUserName.BorderBrush = new SolidColorBrush(Colors.Purple);
}
private void LostFocusAction(object sender, System.Windows.RoutedEventArgs e)
{
InputUserName.Background = new SolidColorBrush(Colors.Red);
InputUserName.BorderBrush = new SolidColorBrush(Colors.Red);
}
答案 2 :(得分:4)
显然你不能在聚焦时更改文本框的背景,而不是在黑暗的主题中(它甚至在系统的集成文本框中也是如此)。
所以我想出了一个解决我问题的解决方案,
我创建了两个完全相同的文本框实例,并将前面的不透明度更改为等于0(完全不可见)。
然后我通过代码连接两个文本框,这样如果正在输入前面的那个(它是不可见的,所以没有人会看到正在写的文本)我编辑后面的文本完全相同。
答案 3 :(得分:1)
您需要评论更大的区域:
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<!--<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBorder" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBorder" Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBorderBrush}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>-->
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>