WPF:只读说文本框和绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1012740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:35:08  来源:igfitidea点击:

WPF: Read only say TextBox and binding

wpfbindingtextbox

提问by abmv

Say I have a grid, I click an object and it displays in a detail screen. I don't want the user to edit some data so I set the TextBox as disabled? Will binding work? Basically what I want is the TextBox to be greyed out or disabled? How about it in WPF? Can someone explain?

假设我有一个网格,我单击一个对象,它会显示在详细信息屏幕中。我不希望用户编辑某些数据,所以我将 TextBox 设置为禁用?绑定会起作用吗?基本上我想要的是文本框变灰或禁用?在WPF中怎么样?有人可以解释一下吗?

回答by Martin Harris

Yes, binding will work with a disabled textbox. For disabling the textbox you have three options:

是的,绑定将适用于禁用的文本框。要禁用文本框,您有三个选项:

  • Set the IsReadOnly property to true. This will not affect the appearance of the textbox, but will stop the user changing the value inside it.

  • Set IsEnabled to false. This will gray out the textbox and stop it from receiving focus

  • Use a label or a textblock. This will place the text on screen without the appearence of being in an editable control at all.

  • 将 IsReadOnly 属性设置为 true。这不会影响文本框的外观,但会阻止用户更改其中的值。

  • 将 IsEnabled 设置为 false。这将使文本框变灰并停止接收焦点

  • 使用标签或文本块。这会将文本放在屏幕上,而根本不会出现在可编辑控件中。

As for binding, this will work the same no matter what you do. Set up the binding as normal in either the Xaml or codebehind and the value will update when the backing property changes as usual (provided you have implemented INotifyPropertyChanged, otherwise it'll only get set once)

至于绑定,无论您做什么,这都将起作用。在 Xaml 或代码隐藏中正常设置绑定,当支持属性照常更改时,值将更新(前提是您已实现 INotifyPropertyChanged,否则它只会设置一次)

回答by Thomas Levesque

There is a IsReadOnlyproperty on the TextBox, just set it to true

IsReadOnlyTextBox 上有一个属性,只需将其设置为 true

回答by Bob King

I would use a <TextBlock/> or a <Label/> to display static data instead of a <TextBox/>.

我会使用 <TextBlock/> 或 <Label/> 来显示静态数据而不是 <TextBox/>。