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.
我使用此模板为用户创建可编辑的矩阵。用户能够在矩阵内从一个单元格导航到另一个单元格,我想突出显示所选文本框中的数据,但它不起作用。我调用TextBox.Focus()和TextBox.SelectAll()来实现效果,但没有。 Focus()有效,但文本永远不会突出显示。
Any help is welcome and appreciated.
欢迎任何帮助和赞赏。
3 个解决方案
#1
12
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 the textBox.SelectAll()
and textBox.Focus()
are called.
好吧,如果有人感兴趣,我的这个问题的解决方案是包括声明e.Handled = true;在事件处理程序方法中调用textBox.SelectAll()和textBox.Focus()。
The problem was that I attached an event handler to the textbox's PreviewKeyDown
event which handles a tunneling event and probably the SelectAll()
and Focus()
calls are ignored without calling the e.Handled = true;
statement.
问题是我将一个事件处理程序附加到文本框的PreviewKeyDown事件,该事件处理隧道事件,并且可能忽略SelectAll()和Focus()调用而不调用e.Handled = true;声明。
Hope it'll help someone.
希望它能帮助别人。
#2
0
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).
如果没有其余的代码,很难说这是否适合您,但我使用您的DataTemplate汇总了一个小样本(减去引用未发布的代码的部分)。
I was able to select all the text in the text boxes by adding a GotFocus event handler to the TextBox in the DataTemplate:
我可以通过向DataTemplate中的TextBox添加GotFocus事件处理程序来选择文本框中的所有文本:
<TextBox
...
GotFocus="textBox_GotFocus"
...>
...
</TextBox>
And the code-behind:
代码隐藏:
private void textBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
textBox.SelectAll();
}
}
Let me know if you are attempting to select all under different circumstances (not when the box receives focus).
如果您尝试在不同情况下选择所有情况(请勿在框中获得焦点时),请告诉我。
#3
-1
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
这是一个非常好的非常简单的解决方案(我不知道它是否适用于您的模板,但试一试):http://social.msdn.microsoft.com/forums/en-US/wpf/thread/564b5731 -af8a-49bf-b297-6d179615819f
#1
12
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 the textBox.SelectAll()
and textBox.Focus()
are called.
好吧,如果有人感兴趣,我的这个问题的解决方案是包括声明e.Handled = true;在事件处理程序方法中调用textBox.SelectAll()和textBox.Focus()。
The problem was that I attached an event handler to the textbox's PreviewKeyDown
event which handles a tunneling event and probably the SelectAll()
and Focus()
calls are ignored without calling the e.Handled = true;
statement.
问题是我将一个事件处理程序附加到文本框的PreviewKeyDown事件,该事件处理隧道事件,并且可能忽略SelectAll()和Focus()调用而不调用e.Handled = true;声明。
Hope it'll help someone.
希望它能帮助别人。
#2
0
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).
如果没有其余的代码,很难说这是否适合您,但我使用您的DataTemplate汇总了一个小样本(减去引用未发布的代码的部分)。
I was able to select all the text in the text boxes by adding a GotFocus event handler to the TextBox in the DataTemplate:
我可以通过向DataTemplate中的TextBox添加GotFocus事件处理程序来选择文本框中的所有文本:
<TextBox
...
GotFocus="textBox_GotFocus"
...>
...
</TextBox>
And the code-behind:
代码隐藏:
private void textBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
textBox.SelectAll();
}
}
Let me know if you are attempting to select all under different circumstances (not when the box receives focus).
如果您尝试在不同情况下选择所有情况(请勿在框中获得焦点时),请告诉我。
#3
-1
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
这是一个非常好的非常简单的解决方案(我不知道它是否适用于您的模板,但试一试):http://social.msdn.microsoft.com/forums/en-US/wpf/thread/564b5731 -af8a-49bf-b297-6d179615819f