你的头发呢. 2023-08-10 11:48 采纳率: 62.5%
浏览 20

.net framework3.5更改TextBox光标颜色

TextBox更改光标颜色
在.net framework高版本可以使用CaretBrush="Red"
但是现在需要兼容3.5在不安装.net framework的情况下,如何做到

.net framework3.5不支持CaretBrush属性

img

  • 写回答

1条回答 默认 最新

  • xlxlxww 2023-08-10 13:36
    关注

    在.NET Framework 3.5中,TextBox控件的光标颜色是由操作系统的默认设置决定的,无法通过控件属性直接更改。但是,你可以通过自定义控件模板来实现更改光标颜色的效果。以下是一种可能的解决方案: 1. 首先,创建一个新的样式文件(例如"CustomTextBoxStyle.xaml"),并将以下代码复制到文件中: xaml x:Key="CustomTextBoxStyle" TargetType="TextBox"> Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" /> Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" /> BorderBrush" Value="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" /> Property="BorderThickness" Value="1" /> Property="KeyboardNavigation.TabNavigation" Value="None" /> Property="HorizontalContentAlignment" Value="Left" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="AllowDrop" Value="true" /> <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst" /> Property="Stylus.IsFlicksEnabled" Value="False" /> <Setter Property="Template"> <Setter.Value> TargetType="TextBoxBase"> :Name="border" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" /> </Border> .Triggers> <Trigger Property="IsEnabled" Value="False"> Name="border" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" /> </Trigger> <Trigger Property="IsKeyboardFocused" Value="True"> TargetName="border" Property="BorderBrush" Value="Red" /> 设置光标颜色 --> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter> </Style> 2. 在你的WPF窗口或应用程序的资源字典中引用该样式文件(例如在App.xaml中): xaml .Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> Dictionary Source="CustomTextBoxStyle.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 3. 现在,你可以在XAML中使用该样式来创建TextBox,并更改光标颜色,如下所示: xaml Style="{StaticResource CustomTextBoxStyle}" Width="200" Height="30" /> 请注意,这种解决方案是通过自定义控件模板来实现的,因此它可能会影响其他TextBox控件的外观和行为。如果需要在不影响其他TextBox控件的情况下更改光标颜色,可能需要进一步调整模板中的其他部分。

    评论

报告相同问题?

问题事件

  • 创建了问题 8月10日

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中