views:

54

answers:

3

How would I change the background color of a TextBox Control in the Default Style Xaml to be a different color when the control is either Disabled or ReadOnly ?

A: 

You can achieve this with triggers in the style:

    <TextBox>
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="IsReadOnly" Value="True">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="True">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
naacal
I get an error, "The attachable property Triggers was not found in Style"
gmcalab
@gmcalab Style.Triggers are not supported in Silverlight; this is WPF-only.
Jay
@Jay, yes I know.
gmcalab
A: 

Im not at a PC at the moment (just mobile) but I think you can edit the template of your control and there are some Visual States for your some controls that define things like disabled states, mouse overs, etc... which you should be able to redefine?

Mark
@Mark, yes, that is my question. How to do this.
gmcalab
do you have expression blend? it makes life SO much easier for this type of thing
Mark
A: 

The way I accomplished this was to create a Converter for the control. When the control is bound to an object it detects if the control is Enabled from this object that it is bound to. Based upon this it sets the background color for the Textbox accordingly.

gmcalab