WPF TextBox.SelectAll () 不起作用
我在我的项目中使用了以下模板:
<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() 有效,但文本永远不会突出显示。
欢迎并赞赏任何帮助。
I have used the following template in my project:
<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>
I used this template to create an editable matrix for the user. The user is able to navigate from cell to cell within the matrix and I would like to highlight the data in the selected textbox but it doesn't work. I called TextBox.Focus () and TextBox.SelectAll () to achieve the effect but nothing. The Focus () works but the text never gets highlighted.
Any help is welcome and appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,如果有人感兴趣,我的这个问题的解决方案是在事件处理程序方法中包含语句
e.Handled = true;
,其中textBox.SelectAll()
和textBox.Focus()
被调用。问题是我将一个事件处理程序附加到文本框的
PreviewKeyDown
事件,该事件处理隧道事件,可能还有SelectAll()
和Focus()
如果不调用e.Handled = true;
语句,调用将被忽略。希望它能帮助某人。
Okay, if anyone is interested, the solution to this problem of mine was to include the statement
e.Handled = true;
in the event handler method where thetextBox.SelectAll()
andtextBox.Focus()
are called.The problem was that I attached an event handler to the textbox's
PreviewKeyDown
event which handles a tunneling event and probably theSelectAll()
andFocus()
calls are ignored without calling thee.Handled = true;
statement.Hope it'll help someone.
如果没有其余的代码,很难说这是否适合您,但我使用您的 DataTemplate 组合了一个小示例(减去引用未发布的代码的部分)。
我可以通过将 GotFocus 事件处理程序添加到 DataTemplate 中的 TextBox 来选择文本框中的所有文本:
并且隐藏代码:
如果您尝试在不同情况下选择所有文本(不是当框接收到文本框时),请告诉我重点)。
Without the rest of your code it's difficult to say whether this will work for you, but I put together a small sample using your DataTemplate (minus the parts that refer to code that wasn't posted).
I was able to select all the text in the text boxes by adding a GotFocus event handler to the TextBox in the DataTemplate:
And the code-behind:
Let me know if you are attempting to select all under different circumstances (not when the box receives focus).
这是一个非常好的非常简单的解决方案(我不知道它是否适用于您的模板,但请尝试一下): http://social.msdn.microsoft.com/forums/en-US/wpf/thread/564b5731-af8a-49bf-b297- 6d179615819f
Here's a very good very simple solution (I don't know if it works for your template, but give it a try): http://social.msdn.microsoft.com/forums/en-US/wpf/thread/564b5731-af8a-49bf-b297-6d179615819f