wpf:在文本框中选择IsReadOnly = true的文本? [英] wpf: Selecting the Text in TextBox with IsReadOnly = true?

查看:208
本文介绍了wpf:在文本框中选择IsReadOnly = true的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个TextBox.此文本框位于 DataTemplate :

I have this TextBox. This TextBox is located in DataTemplate:

<DataTemplate x:Key="myTemplate">
    <TextBox  Text="{Binding Path=FullValue, Mode=TwoWay}" IsEnabled="False"  />
       ...

,并且我希望允许用户选择其中的整个文本(也可以通过单击文本框"来选择).而且我不想使用任何 后面的代码.

and I want to allow User to select the whole text inside it (optionally by clicking on the Textbox). And I don't want to use any code behind.

该怎么做?预先感谢.

推荐答案

使用IsReadOnly属性代替IsEnabled允许用户选择文本.另外,如果不应该对其进行编辑,那么OneWay绑定就足够了.

Using the IsReadOnly property instead of IsEnabled allows the user to select text. Also, if it shouldn't be edited, a OneWay binding should be enough.

XAML的想法不是完全替代后台代码.最重要的是,您尝试在背后的代码中仅包含特定于UI的代码,而不是业务逻辑.话虽这么说,选择所有文本是特定于UI的,并且在后面的代码中也没有什么坏处.为此,请使用myTextBox.SelectAll().

The idea of XAML isn't to completely replace code-behind. Most important is that you try to have only UI-specific code in the code-behind, not business logic. That being said, selecting all text is UI-specific and doesn't hurt in the code-behind. Use myTextBox.SelectAll() for that.

这篇关于wpf:在文本框中选择IsReadOnly = true的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆