user1018711
user1018711

Reputation:

Change color of control to default

i do have a textbox. When there is error i set borderbrush to new SolidColorBrush(Colors.Red). When error is fixed, i want to switch to default color of border of a textbox. I am doing it in codebehind not xaml.

However it is system dependent. I noticed there is something like

SystemColor.ActiveControl etc. Should i use these and if yes, which one is default border of textbox?

Also i noticed there is something like Textbox.borderbrushproperty.defaultmetadata.defaultvalue, which i did not manage to work.

Any ideas how to switch to default borderbrush? thank you.

Upvotes: 2

Views: 3515

Answers (4)

carny666
carny666

Reputation: 2420

Maybe I'm a little late to the party but I think you're asking how to change a color to the system default of control?

someControl.BackColor = SystemColors.Control;

or

sslEditMode.BackColor = SystemColors.ButtonFace;

Upvotes: 0

Rachel
Rachel

Reputation: 132558

Do you have to do it in code-behind? If not, you could use a Trigger which will only change the border color while the trigger condition is met.

<Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=MyUserControl, Path=HasErrors}">
            <Setter Property="BorderBrush" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Upvotes: 2

Mike Hofer
Mike Hofer

Reputation: 17022

Try:

control.BackColor = Color.FromKnownColor(KnownColor.Window);

For more information, see Color.FromKnownColor on MSDN.

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124642

Why not just save the initial value at startup and use that? You should be able to use the system colors, but if you ever change the default color this will continue to work.

private Color _defBtnColor;
public MyUserControl()
{
    _defBtnColor = someButton.Foreground;
}  

private void SetBackToDefault()
{
    someButton.Foreground = _defBtnColor;
}

Upvotes: 1

Related Questions