textbox样式 边框颜色改变后又变回来怎么处理

qq14923349 2017-06-03 03:29:25
写了一个textbox的样式
然后设置了trigger
在ismouseover 和isfocused设置了borderthickness为1 borderbrush为黑色

目前效果是鼠标移过去是黑色,不够一会就自动变回原本的渐进色边框

这个怎么回事?
如果设置borderthickness 为2 就不会存在这个问题 但是不好看
...全文
521 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq14923349 2017-06-08
  • 打赏
  • 举报
回复
引用 18 楼 duanzi_peng 的回复:
那就不清楚了,这是我这边的效果,和你的样式一样,
你看下边框 我要的是四边同一颜色的边框 宽度为1 鼠标移进边框变红色 移开变边框蓝色 四边同一颜色的边框 宽度为1 不知道为什么是渐进色的
exception92 2017-06-08
  • 打赏
  • 举报
回复
那就不清楚了,这是我这边的效果,和你的样式一样,
qq14923349 2017-06-08
  • 打赏
  • 举报
回复
引用 16 楼 duanzi_peng 的回复:
[quote=引用 15 楼 qq14923349 的回复:] [quote=引用 14 楼 duanzi_peng 的回复:] [quote=引用 13 楼 qq14923349 的回复:] 我设置BorderThickness为1后 不明白边框怎么有渐进色出来,整页代码在上面
样式我已经测试过了,只要鼠标悬浮 样式就不会改变;[/quote] 能取消鼠标悬浮吗[/quote] 取消鼠标悬浮 变为原来的呀
引用 15 楼 qq14923349 的回复:
[quote=引用 14 楼 duanzi_peng 的回复:] [quote=引用 13 楼 qq14923349 的回复:] 我设置BorderThickness为1后 不明白边框怎么有渐进色出来,整页代码在上面
样式我已经测试过了,只要鼠标悬浮 样式就不会改变;[/quote] 能取消鼠标悬浮吗[/quote] 什么意思? trigger 一般情况下 都是value为true的时候设置一些特效。[/quote]
<Trigger Property="IsFocused" Value="true">
                                <Setter Property="BorderThickness" Value="1"/>
                                <Setter Property="BorderBrush"  Value="Black"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="BorderThickness" Value="1"/>
                                <Setter Property="BorderBrush"  Value="Black"/>
                            </Trigger>
我就是没搞清楚为啥我这么设置以后,鼠标移入点击textbox,生效半秒,然后迅速就恢复渐进色形态
exception92 2017-06-08
  • 打赏
  • 举报
回复
引用 15 楼 qq14923349 的回复:
[quote=引用 14 楼 duanzi_peng 的回复:] [quote=引用 13 楼 qq14923349 的回复:] 我设置BorderThickness为1后 不明白边框怎么有渐进色出来,整页代码在上面
样式我已经测试过了,只要鼠标悬浮 样式就不会改变;[/quote] 能取消鼠标悬浮吗[/quote] 取消鼠标悬浮 变为原来的呀
引用 15 楼 qq14923349 的回复:
[quote=引用 14 楼 duanzi_peng 的回复:] [quote=引用 13 楼 qq14923349 的回复:] 我设置BorderThickness为1后 不明白边框怎么有渐进色出来,整页代码在上面
样式我已经测试过了,只要鼠标悬浮 样式就不会改变;[/quote] 能取消鼠标悬浮吗[/quote] 什么意思? trigger 一般情况下 都是value为true的时候设置一些特效。
exception92 2017-06-08
  • 打赏
  • 举报
回复
这是因为你用了 ClassicBorderDecorator 作为外层装饰,而且设置了BorderStyle,这是默认的,除非用普通Border 来替换ClassicBorderDecorator。
exception92 2017-06-07
  • 打赏
  • 举报
回复
引用 6 楼 qq14923349 的回复:
<Trigger Property="IsFocused" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger> <Trigger Property="IsMouseOver" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger>
IsMouseOver 为false ,鼠标移开 当然会没效果了。 如果鼠标一直悬浮,就不会变回来。 况且 setter里也没有指定TargetName,是你鼠标移开造成的。
qq14923349 2017-06-07
  • 打赏
  • 举报
回复

<Window.Resources>
		<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
			<Setter Property="BorderBrush" Value="{x:Static Microsoft_Windows_Themes:ClassicBorderDecorator.ClassicBorderBrush}"/>
			<Setter Property="BorderThickness" Value="2"/>
			<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
			<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
			<Setter Property="Padding" Value="1"/>
			<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
			<Setter Property="HorizontalContentAlignment" Value="Left"/>
			<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
			<Setter Property="AllowDrop" Value="true"/>
			<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
			<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
			<Setter Property="Template">
				<Setter.Value>
					<ControlTemplate TargetType="{x:Type TextBox}">
						<Microsoft_Windows_Themes:ClassicBorderDecorator x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" BorderStyle="Sunken" Background="{TemplateBinding Background}">
							<ScrollViewer x:Name="PART_ContentHost" BorderThickness="1" Background="#00F51919" BorderBrush="#FFEF7B38"/>
						</Microsoft_Windows_Themes:ClassicBorderDecorator>
						<ControlTemplate.Triggers>
							<Trigger Property="IsEnabled" Value="false">
								<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
								<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
							</Trigger>
							<Trigger Property="IsFocused" Value="false">
								<Setter Property="BorderThickness" Value="1"/>
								<Setter Property="BorderBrush"  Value="Black"/>
							</Trigger>
							<Trigger Property="IsMouseOver" Value="false">
								<Setter Property="BorderThickness" Value="1"/>
								<Setter Property="BorderBrush"  Value="Black"/>
							</Trigger>
						</ControlTemplate.Triggers>
					</ControlTemplate>
				</Setter.Value>
			</Setter>
		</Style>
	</Window.Resources>

	<Grid x:Name="LayoutRoot">
		<TextBox Height="76" Margin="126,115,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="246" Style="{DynamicResource TextBoxStyle1}"/>
	</Grid>
qq14923349 2017-06-07
  • 打赏
  • 举报
回复
<Window.Resources> <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}"> <Setter Property="BorderBrush" Value="{x:Static Microsoft_Windows_Themes:ClassicBorderDecorator.ClassicBorderBrush}"/> <Setter Property="BorderThickness" Value="2"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="Padding" Value="1"/> <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="AllowDrop" Value="true"/> <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Microsoft_Windows_Themes:ClassicBorderDecorator x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" BorderStyle="Sunken" Background="{TemplateBinding Background}"> <ScrollViewer x:Name="PART_ContentHost" BorderThickness="1" Background="#00F51919" BorderBrush="#FFEF7B38"/> </Microsoft_Windows_Themes:ClassicBorderDecorator> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> <Trigger Property="IsFocused" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger> <Trigger Property="IsMouseOver" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid x:Name="LayoutRoot"> <TextBox Height="76" Margin="126,115,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="246" Style="{DynamicResource TextBoxStyle1}"/> </Grid>
qq14923349 2017-06-07
  • 打赏
  • 举报
回复
引用 7 楼 duanzi_peng 的回复:
[quote=引用 6 楼 qq14923349 的回复:] <Trigger Property="IsFocused" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger> <Trigger Property="IsMouseOver" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger>
给你贴个样式,自己按自己的要求改吧。

 <Style TargetType="{x:Type TextBox}">
        <Setter Property="Template">
          <Setter.Value>
             <ControlTemplate TargetType="{x:Type TextBoxBase}">
                  <Border x:Name="border" BorderBrush="Red" BorderThickness="1">
                    <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalAlignment="Stretch"></ScrollViewer>
                  </Border>
                  <ControlTemplate.Triggers>
                     <Trigger Property="IsFocused" Value="True">
                       <Setter Property="BorderBrush" TargetName="border"  Value="Green"></Setter>
                       <Setter Property="BorderThickness" TargetName="border"  Value="2"></Setter>
                     </Trigger>
                     <Trigger Property="IsEnabled" Value="False">
                       <Setter Property="BorderBrush" TargetName="border"  Value="Gray"></Setter>
                       <Setter Property="BorderThickness" TargetName="border"  Value="2"></Setter>
                     </Trigger>
                  </ControlTemplate.Triggers>
             </ControlTemplate>
          </Setter.Value>
        </Setter>
     </Style>
[/quote] 老大这种样式肯定没问题,只是我不知道我的怎么回事,是不是我那个地方概念错了,想纠正一下,下面我贴出该style,blend生成很杂
exception92 2017-06-07
  • 打赏
  • 举报
回复
引用 6 楼 qq14923349 的回复:
<Trigger Property="IsFocused" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger> <Trigger Property="IsMouseOver" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger>
给你贴个样式,自己按自己的要求改吧。

 <Style TargetType="{x:Type TextBox}">
        <Setter Property="Template">
          <Setter.Value>
             <ControlTemplate TargetType="{x:Type TextBoxBase}">
                  <Border x:Name="border" BorderBrush="Red" BorderThickness="1">
                    <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalAlignment="Stretch"></ScrollViewer>
                  </Border>
                  <ControlTemplate.Triggers>
                     <Trigger Property="IsFocused" Value="True">
                       <Setter Property="BorderBrush" TargetName="border"  Value="Green"></Setter>
                       <Setter Property="BorderThickness" TargetName="border"  Value="2"></Setter>
                     </Trigger>
                     <Trigger Property="IsEnabled" Value="False">
                       <Setter Property="BorderBrush" TargetName="border"  Value="Gray"></Setter>
                       <Setter Property="BorderThickness" TargetName="border"  Value="2"></Setter>
                     </Trigger>
                  </ControlTemplate.Triggers>
             </ControlTemplate>
          </Setter.Value>
        </Setter>
     </Style>
qq14923349 2017-06-07
  • 打赏
  • 举报
回复
引用 14 楼 duanzi_peng 的回复:
[quote=引用 13 楼 qq14923349 的回复:] 我设置BorderThickness为1后 不明白边框怎么有渐进色出来,整页代码在上面
样式我已经测试过了,只要鼠标悬浮 样式就不会改变;[/quote] 能取消鼠标悬浮吗
exception92 2017-06-07
  • 打赏
  • 举报
回复
引用 13 楼 qq14923349 的回复:
我设置BorderThickness为1后 不明白边框怎么有渐进色出来,整页代码在上面
样式我已经测试过了,只要鼠标悬浮 样式就不会改变;
qq14923349 2017-06-07
  • 打赏
  • 举报
回复
引用 11 楼 duanzi_peng 的回复:
[quote=引用 6 楼 qq14923349 的回复:] <Trigger Property="IsFocused" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger> <Trigger Property="IsMouseOver" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger>
IsMouseOver 为false ,鼠标移开 当然会没效果了。 如果鼠标一直悬浮,就不会变回来。 况且 setter里也没有指定TargetName,是你鼠标移开造成的。[/quote] 我设置BorderThickness为1后 不明白边框怎么有渐进色出来,整页代码在上面
qq14923349 2017-06-07
  • 打赏
  • 举报
回复

<Window
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Classic"
	x:Class="textbox1.MainWindow"
	x:Name="Window"
	Title="MainWindow"
	Width="640" Height="480">

	<Window.Resources>
		<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
			<Setter Property="BorderBrush"  Value="Red"/>
			<Setter Property="BorderThickness" Value="1"/>
			<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
			<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
			<Setter Property="Padding" Value="1"/>
			<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
			<Setter Property="HorizontalContentAlignment" Value="Left"/>
			<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
			<Setter Property="AllowDrop" Value="true"/>
			<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
			<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
			<Setter Property="Template">
				<Setter.Value>
					<ControlTemplate TargetType="{x:Type TextBox}">
						<Microsoft_Windows_Themes:ClassicBorderDecorator x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" BorderStyle="Sunken" Background="{TemplateBinding Background}">
							<ScrollViewer x:Name="PART_ContentHost" BorderThickness="1" Background="#00F51919" BorderBrush="#FFEF7B38"/>
						</Microsoft_Windows_Themes:ClassicBorderDecorator>
						<ControlTemplate.Triggers>
							<Trigger Property="IsEnabled" Value="false">
								<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
								<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
							</Trigger>
							<Trigger Property="IsFocused" Value="true">
								<Setter Property="BorderThickness" Value="1"/>
								<Setter Property="BorderBrush"  Value="Black"/>
							</Trigger>
							<Trigger Property="IsMouseOver" Value="true">
								<Setter Property="BorderThickness" Value="1"/>
								<Setter Property="BorderBrush"  Value="Black"/>
							</Trigger>
						</ControlTemplate.Triggers>
					</ControlTemplate>
				</Setter.Value>
			</Setter>
		</Style>
	</Window.Resources>

	<Grid x:Name="LayoutRoot">
		<TextBox Height="76" Margin="126,115,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="246" Style="{DynamicResource TextBoxStyle1}"/>
		<Button Content="Button" HorizontalAlignment="Right" Height="32" Margin="0,102,125,0" VerticalAlignment="Top" Width="53"/>
	</Grid>
</Window>
alex_suen 2017-06-06
  • 打赏
  • 举报
回复
Talk is cheap, show me the code.
qq14923349 2017-06-06
  • 打赏
  • 举报
回复
<Trigger Property="IsFocused" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger> <Trigger Property="IsMouseOver" Value="false"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="Black"/> </Trigger>
exception92 2017-06-05
  • 打赏
  • 举报
回复
把代码贴出来看看
exception92 2017-06-05
  • 打赏
  • 举报
回复
这应该是“像素未对齐” 原因,设置模板的 SnapsToDevicePixels 属性为True。
Mise_V 2017-06-03
  • 打赏
  • 举报
回复
代码复制给我看看
qq14923349 2017-06-03
  • 打赏
  • 举报
回复
我需要移动过去一直保留黑色或者其他颜色边框

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧