我在我的项目中使用了以下模板:
<DataTemplate
x:Key="textBoxDataTemplate">
<TextBox
Name="textBox"
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"
Tag="{Binding}"
PreviewKeyDown="cellValueTextBoxKeyDown">
<TextBox.Text>
<MultiBinding
Converter="{StaticResource intToStringMultiConverter}">
<Binding
Path="CellValue"
Mode="TwoWay">
<Binding.ValidationRules>
<y:MatrixCellValueRule
MaxValue="200" />
</Binding.ValidationRules>
</Binding>
<Binding
RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type y:MatrixGrid}}"
Path="Tag"
Mode="OneWay" />
</MultiBinding>
</TextBox.Text>
</TextBox>
</DataTemplate>
我使用这个模板为用户创建了一个可编辑的矩阵。用户可以在矩阵中从一个单元格导航到另一个单元格,我想突出显示所选文本框中的数据,但它不起作用。我调用了TextBox.Focus() 和TextBox.SelectAll() 实现了效果但是没有。 Focus () 有效,但文本永远不会突出显示。
欢迎和感谢任何帮助。
最佳答案
好的,如果有人感兴趣,我这个问题的解决方案是在调用 e.Handled = true;
和 textBox.SelectAll()
的事件处理程序方法中包含语句 textBox.Focus()
。
问题是我将事件处理程序附加到文本框的 PreviewKeyDown
事件,该事件处理隧道事件并且可能忽略 SelectAll()
和 Focus()
调用而不调用 e.Handled = true;
语句。
希望它会帮助某人。
关于textbox - WPF TextBox.SelectAll () 不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1613122/