Home > database >  WPF Button change color when disabled
WPF Button change color when disabled

Time:04-13

The code below is a snippet from a programme. The button that is pressed is now set to red.

private void Disk_click(object sender, RoutedEventArgs e)
        { 
            ((Button)sender).Background = Brushes.Red;
            ((Button)sender).Foreground = Brushes.Red;
                 // here is the button colored red

            ((Button)sender).IsEnabled = false;
                 // here is the button again grey
}

The intention is that when a button is pressed, the colour will change and then the button will be disabeld. The colour must be preserved on the button. The colours that can occur are: red, blue, green and yellow.

But if I disable the button, the colour is not retained. The colour must be adjustable from this c# code. Does anyone know how I can solve this? Or what I am doing wrong?

All help is highly appreciated!

CodePudding user response:

It would be more accurate to edit the button design on the xaml side instead of the code behind. You can use the Style property for this.

In case IsHitTestVisible is false, you can set the controls you want according to your taste when it is true.

<Button Name="Button1" Width="100"
            Height="20"
            Content="Click"
            Click="Disk_click">
        <Button.Style>
            <Style TargetType="Button">
                <Style.Triggers>
                    <Trigger Property="IsHitTestVisible" Value="False">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                    <Trigger Property="IsHitTestVisible" Value="True">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

CodePudding user response:

The button colour is being overridden by the Disabled style which is set in the Xaml.

In the Xaml code of the button, you can set the disabled Background & Foreground colour which will allow it to persist.

CodePudding user response:

Have you considered using a Controltemplate with Triggers? Usually requierments like yours are achieved using Templates and Styles. A Controltemplate might look something like this:

<ControlTemplate   x:Key="ButtonControlTemplate" TargetType="{x:Type Button}">
        <Border  x:Name="border" Height="Auto" Width="65"  BorderThickness="0" Background="Transparent">
            <StackPanel x:Name="Control" Margin="5,0,5,0" Orientation="Horizontal" MinHeight="25" VerticalAlignment="Center">
                <TextBlock VerticalAlignment="Center" x:Name="Icon" FontSize="16"  Foreground="Red"></TextBlock>
                <ContentPresenter x:Name="contentPresenter" Margin="5,0,0,0" VerticalAlignment="Center"></ContentPresenter>
            </StackPanel>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="border" Property="Background" Value="#FFF4F4F4" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
                <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
                <Setter Property="Foreground" Value="#FF838383"/>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsEnabled" Value="True"/>
                    <Condition Property="IsMouseOver" Value="True"/>
                </MultiTrigger.Conditions>
                <Setter TargetName="border" Property="Background" Value="#FFF4F4F4" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

If it must be done with C# code you could try something like this:

if(myButton.IsEnabled == true)
        {
            //do logic here
            // Button Red
        } else
        {
            //do logic here
            //Button Gray
        }

Hope i could help.

  • Related