WPF TextBox.SelectAll () 不起作用

发布于 2024-08-08 21:45:55 字数 1296 浏览 7 评论 0原文

我在我的项目中使用了以下模板:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

完美的未来在梦里 2024-08-15 21:45:55

好的,如果有人感兴趣,我的这个问题的解决方案是在事件处理程序方法中包含语句 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 the textBox.SelectAll() and textBox.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 the SelectAll() and Focus() calls are ignored without calling the e.Handled = true; statement.

Hope it'll help someone.

鯉魚旗 2024-08-15 21:45:55

如果没有其余的代码,很难说这是否适合您,但我使用您的 DataTemplate 组合了一个小示例(减去引用未发布的代码的部分)。

我可以通过将 GotFocus 事件处理程序添加到 DataTemplate 中的 TextBox 来选择文本框中的所有文本:

<TextBox 
    ...
    GotFocus="textBox_GotFocus"
    ...>
...
</TextBox>

并且隐藏代码:

    private void textBox_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if (textBox != null)
        {
            textBox.SelectAll();
        }
    }

如果您尝试在不同情况下选择所有文本(不是当框接收到文本框时),请告诉我重点)。

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:

<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).

夏有森光若流苏 2024-08-15 21:45:55

这是一个非常好的非常简单的解决方案(我不知道它是否适用于您的模板,但请尝试一下): 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文