Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

HTML
 <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..
Posted
Updated 22-Aug-11 19:38pm
v2

1 solution

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.
HTML
<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>
 
Share this answer
 
Comments
somasekhar_37 23-Aug-11 2:17am    
Thank you..that worked for the "textbox" background property changed..but when i click button with changed value in the textbox to bind the value in the gridview ... the old value of the textbox is binding again in the gridview its not taking the changed value in the textbox.. can you please help me how to bind the changed textbox value to the gridview..
somasekhar_37 23-Aug-11 2:21am    
not only the above... the gridview is not at all getting bind with the value=18 from the textbox..please tell me how to resolve it..thank you
Simon Bang Terkildsen 23-Aug-11 2:24am    
I'm not sure I understand correctly, but if I do then <TextBox Text="{Binding Age}" .../> should do the trick
somasekhar_37 23-Aug-11 2:46am    
i have tried it..it does not reslove the thing..i mean when i try to bind the gridview at first with value=18 its taking as "0"..else the gridview is taking the previous value binded to it from textbox..
..In my scenario the "textbox" is not placed inside the gridview or gridview structure..am trying to bind the values entered in the textbox(placed in a tabcontrol-tab1) to the gridview in the tabcontrol(placed in another tab-tab2)...i hope you get my context..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900