在文本框中使用控件模板实现数据触发

编程入门 行业动态 更新时间:2024-10-25 18:34:01
本文介绍了在文本框中使用控件模板实现数据触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是WPF的新手....我尝试为文本框同时实现controltemplate和datatriggers..当我通过datatriggers输入的值不是"18"时,我想更改文本框的背景色. (我希望文本框的控件属性能够按原样工作)如何实现....编写的Xaml代码如下:

I''m new to WPF....I have tried implementing controltemplate and datatriggers both for textbox..I want to change the background color of the textbox when the value entered in that is not "18" through datatriggers..(i want the controlproperty for the textbox to work as it is)how can i achieve it.... the Xaml code written is as follows:

<textbox x:uid="txtagevals" x:name="txtAge" height="25" width="80" removed="Wheat" borderthickness="1" borderbrush="Black" xmlns:x="#unknown"> <textbox.template> <controltemplate x:uid="txtagevals" targettype="{x:Type TextBox}"> <border removed="{TemplateBinding Background}"> BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5"> <scrollviewer x:name="PART_ContentHost" /> </border> <controltemplate.triggers> <datatrigger binding="{Binding Age}" value="18"> <setter targetname="" property="Text" value="Green" /> </datatrigger> </controltemplate.triggers> </controltemplate> </textbox.template> </textbox>

谁能帮我...谢谢..

can anybody please help me...Thank you..

推荐答案

使用ControlTemplate时,您将要更改控件的外观,可以使用混合以获取原始图像,这样您就可以拥有完全相同的图像.但是,在您使用ControlTemplate的情况下,您会过分使用,只需使用Setter将背景颜色设置为Text不为"18"时的颜色,然后在样式"上触发即可将背景为"18"时更改为白色. 例如 When you use a ControlTemplate you''re going to change the look of the control, you can use Blend to get the original so you can have the exact same. But in you case using a ControlTemplate is overkill just use a Setter to set the background color to the color for when Text is not "18" and then make a trigger on the Style to change the Background to White when it is "18". E.g. <Style TargetType="{x:Type TextBox}"> <Setter Property="Background" Value="Red"/> <Style.Triggers> <Trigger Property="Text" Value="18"> <Setter Property="Background" Value="White"/> </Trigger> </Style.Triggers> </Style>

更多推荐

在文本框中使用控件模板实现数据触发

本文发布于:2023-11-11 23:45:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   框中   文本   模板   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!