在文本框中使用控件模板实现数据触发 [英] Implementing datatriggers with controltemplates in textbox

查看:111
本文介绍了在文本框中使用控件模板实现数据触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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>


这篇关于在文本框中使用控件模板实现数据触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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