简体   繁体   English

WPF 禁用时按钮改变颜色

[英]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.颜色必须可从此 c# 代码进行调整。 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.编辑 xaml 侧的按钮设计而不是后面的代码会更准确。 You can use the Style property for this.您可以为此使用 Style 属性。

In case IsHitTestVisible is false, you can set the controls you want according to your taste when it is true.如果 IsHitTestVisible 为 false,当它为 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.按钮颜色被 Xaml 中设置的禁用样式覆盖。

In the Xaml code of the button, you can set the disabled Background & Foreground colour which will allow it to persist.在按钮的 Xaml 代码中,您可以设置禁用的背景和前景色,这将允许它持续存在。

Have you considered using a Controltemplate with Triggers?您是否考虑过将 Controltemplate 与触发器一起使用? Usually requierments like yours are achieved using Templates and Styles. A Controltemplate might look something like this:通常像您这样的要求是使用模板和 Styles 实现的。Controltemplate 可能看起来像这样:

<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:如果必须使用 C# 代码完成,您可以尝试这样的操作:

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;结果;

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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