简体   繁体   中英

WPF Button change color when disabled

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!

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>

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.

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.

Following button style would be helpful;

<Window
x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">

<Window.Resources>
    <Style x:Key="MyButtonStyle" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Background" Value="LightGray" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter
                            x:Name="MyContentPresenter"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Content="{TemplateBinding Content}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <Button
        Width="75"
        Height="35"
        Margin="5"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Click="Button_Click"
        Content="Button"
        Style="{StaticResource MyButtonStyle}" />
</Grid>

On Click;

    private void Button_Click(object sender, RoutedEventArgs e)
    {
           ((Button)sender).IsEnabled = false;
    }

Result;

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM