仅在WPF中为禁用的文本框设置默认文本
当我需要在WPF应用程序中使用禁用> TextBox 控件时,我只需要设置默认文本。这是启用的控件可能包含空文本,但是当禁用控件使用空文本禁用时,应显示binden bended值,例如“无数据”。
在WPF中有什么正确的方法吗?
I have a case when I need to set default text only for disabled TextBox
controls in my WPF application. That is enabled controls may contain empty text, but when control is disabled with empty text there should be displayed binded value e.g. "No data".
Is there any proper way to do this in WPF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用这样的行为:
用法示例:
NB!您可以在行为中定义
displayMode
属性,以设置默认文本显示。如果您设置displayDefaultTextMode.textboxtextempty
如果TexBox文本为空或空,则将设置默认文本。如果您设置displayDefaultTextMode.textboxdisabledandtextempty
Defualt Text将仅设置为带有空文本的禁用文本框。希望这对您有帮助。
You can use behavior like this:
Usage example:
N.B! You can define
DisplayMode
property in the behavior to set up default text appearence. If you setDisplayDefaultTextMode.TextBoxTextEmpty
default text will be set if texbox text is null or empty. And if you setDisplayDefaultTextMode.TextBoxDisabledAndTextEmpty
defualt text will be set only to the disabled textbox with empty text.Hope it will be helpful for you.
您不需要转换器,可以在纯XAML中完成。
首先,将光标放在文本框上,在属性窗格中选择其他 - >模板 - >转换为新资源。这将模板删除您的控件,从而为您提供一些编辑。只需将
part_contenthost
元素更改为包含您默认文本的文本块:现在只需使用样式将该控制模板应用于禁用控件时:
现在,当您将类似的内容放入XAML中时,则现在
。 ..您得到这个:
You don't need a converter for this, it can be done in pure XAML.
First, place the cursor over a TextBox, and in the Properties pane select Miscellaneous -> Template -> Convert to New Resource. That will template out your control, giving you something to edit. Simply change the
PART_ContentHost
element to a TextBlock containing your default text:And now just use a style to apply that ControlTemplate to whenever the control is disabled:
So now when you put something like this in your XAML:
...you get this: