- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个带有自定义样式的 TextBox 输入字段:
<TextBox Style="{StaticResource SettingsTextBoxHint}" KeyDown="textBoxInput_KeyDown" PreviewKeyDown="textBoxInput_PreviewKeyDown" Name="TextBoxInput" Text="{Binding TextBoxInput, Mode=TwoWay}" Tag="{lex:LocText TypeAMessageHere}" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" FontSize="14" HorizontalAlignment="Stretch" VerticalAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" Margin="0,0,33,0"/>
我添加了自定义样式:
<Style TargetType="{x:Type TextBox}" x:Key="SettingsTextBoxHint" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border x:Name="Border"
BorderThickness="{TemplateBinding BorderThickness}"
Background="White"
Padding="1,2,5,2"
BorderBrush="{StaticResource PrimaryColor}"
ToolTip="{TemplateBinding ToolTip}">
<Grid>
<TextBox Text="{Binding Path=Text,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
x:Name="textSource"
Background="Transparent"
BorderBrush="Transparent"
TextWrapping="{TemplateBinding TextWrapping}"
Panel.ZIndex="2"
FontSize="12">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border x:Name="Border"
Background="Transparent"
BorderBrush="Transparent"
CornerRadius="0">
<ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TextBox.Style>
</TextBox>
<TextBox Margin="0,3,0,0" BorderThickness="0" Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="{StaticResource RobotoRegularFont}" />
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="#cccccc"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="BorderBrush" Value="DarkGray"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="textSource" Property="FocusManager.FocusedElement" Value="{Binding ElementName=textSource}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
重点是使 TextBox 透明并添加通过 Tag 设置文本的提示功能。
一切都如我所愿。我遇到的问题是我正在编写一个新功能,其中我需要光标在 TextBox 中的位置。但是 SelectionStart 或 CaretIndex 总是返回 0 值。如果我删除我的样式,我会得到正确的值。
谁能告诉我我错过了什么?
最佳答案
问题是您正在 TextBox
中使用 TextBox
。用户与内部文本框交互,其文本、插入符号位置等与外部文本框没有任何关系。
让我为你整理一下你的风格:
<Style x:Key="SettingsTextBoxHint"
TargetType="{x:Type TextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="BorderBrush"
Value="{StaticResource PrimaryColor}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border x:Name="Border"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
Padding="{TemplateBinding Padding}">
<Grid>
<ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center" />
<TextBlock x:Name="Hint"
Margin="3,1"
Text="{TemplateBinding Tag}"
FontStyle="Italic"
Foreground="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"
Visibility="Hidden" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border"
Property="Background"
Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter TargetName="Hint" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
要在 TextBox
中找到光标位置,您可以使用 Mouse.GetPosition(your_text_box)
。要获取文本中的 caret 位置,请使用 CaretIndex
属性。但是请注意,CaretIndex
是*不是* 依赖属性,因此它不会引发更改通知。因此,您不能绑定(bind)到它并期望更新绑定(bind)目标。
关于c# - 自定义样式的 TextBox SelectionStart 始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48323294/
这个问题在这里已经有了答案: selectionStart-End with textareas (2 个答案) 关闭 10 年前。 据我了解,selectionStart 必须从输入文本或文本区域
我正在尝试对文本框使用 SelectionStart 和 SelectionLength 属性。它没有效果,但也没有错误。它实际上是后台工作程序 ProgressChanged 方法的一部分,但我已经
我需要向用户显示,就像在 notepad.exe 中一样,他在光标上的文本的选择开始和长度。 选择长度没有问题,因为 Richtextbox 支持带有开始和结束的选择属性。 http://msdn.m
我正在为彩色文本使用 RichTextBox。假设我想为文本的不同部分使用不同的颜色。到目前为止一切正常。 我目前遇到 RichTextBox 的 SelectionStart 属性问题。我已经为 R
我在让下面的脚本与我的文本编辑器一起工作时遇到了一些困难。我不确定出了什么问题,但 selectionStart 和 selectionEnd 返回时未定义。 它应该在可编辑的 iframe 中获取突
在我的简单书签中,我调用了文档的所有 input 元素,然后尝试访问每个元素的 selectionStart: javascript: (function () { var inps=documen
我遇到了这个烦人的问题,我似乎无法在文本区域中获取所选文本的开始和结束索引,我得到的所有内容都是未定义的,如下所示: $('#myarea').selectionStart; // return un
我正在使用 selectionStart和 selectionEnd为了获得文本选择的起点和终点。 代码:https://codesandbox.io/s/busy-gareth-mr04o 但是,我
如果我输入 a然后 进入 IE11 文本区域并记录 selectionStart ,它是 2 (这是我所期望的)。但是,如果我以编程方式将 textarea 的值设置为 'a\n'相反,selecti
return layout.getLineForOffset(selectionStart); 这条线导致我的应用崩溃。 我正在用它来获取多行编辑文本中的当前光标行。像这样。 public int g
我正在尝试获取用户在文本框上单击的插入符号位置。我想在用户点击输入的位置添加一定的文本。我正在尝试在 input 的 onFocus 方法中执行此操作。但是,e.target.selectionSta
我正在尝试检测用户在关注 HTML 输入元素时点击的完整单词,然后查看它是否是 URL。我正在使用以下方法: let el = this.reminderInput.nativeElement var
每当我突出显示selectionStart = 0;时,该代码不执行。但是,如果我突出显示文本区域中第一个字符前面的一个字符,则代码将起作用。如果我突出显示文本区域中的第一个字符,有什么想法可以使此代
$(element)[0].selectionStart 似乎只适用于文本区域。是否有非文本区域的替代方案 我正在尝试将字符串包装在标记(粗体、斜体...)中的 DOM 元素中 另外,如何取消包装?
我有一个多行文本框,我想在文本框下方的标签中显示用户当前的行号和列号。看起来我可以使用 SelectionStart 属性、GetLineFromCharIndex 方法和 GetFirstCharI
我一直在努力使用 textarea 的 selectionStart 和 selectionEnd 属性,以使它们与 contenteditable div 元素一起使用。我在谷歌和SO上检查了很多相
我正在将一个 angularJS 文本插入器实现到一个文本区域,该文本区域将指定的文本插入到文本区域中光标的位置。经过一些谷歌搜索后,我在网上找到了这个示例。 指令的主体在这里 app.directi
使用这段代码,我可以检查 Firefox 中文本区域中的插入符位置: document.getElementById("myTextArea").selectionStart 这在 IE 8 中不起作
我有一个带有自定义样式的 TextBox 输入字段: 我添加了自定义样式:
如何根据鼠标位置设置 WinForms TextBox 的 SelectionStart?我正在扩展 TextBox 并希望在用户右键单击 TextBox 时将 carot 设置在鼠标点下方的位置(即
我是一名优秀的程序员,十分优秀!