不断更改 wpf 中按钮的背景

本文关键字:按钮 背景 wpf | 更新日期: 2023-09-27 18:37:22

我的窗户上有一个按钮。 默认情况下,按钮的背景颜色为蓝色。 单击按钮时,颜色应更改为红色。 但我看到背景颜色正在变为红色,但它不是永久性的。

下面是类代码:

/// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        bool _highLow;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
           _highLow = false;
        }
        public bool RiskHighLowMedium
        {
            get { return _highLow; }
            set { _highLow= value ;
            this.OnPropertyChanged("RiskHighLowMedium");
            this.OnPropertyChanged("BackGround");
            }
        }

         //background Dependency Property
        public static readonly DependencyProperty BackgroundProperty;

        /// <summary>
        /// static constructor
        /// </summary>
        static MainWindow()
        {
            BackgroundProperty = DependencyProperty.Register("Background", typeof(Brush), typeof(MainWindow));
        }

        /// <summary>
        /// Background Dependency
        /// Property
        /// </summary>
        public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RiskHighLowMedium = true;
            Background = RiskHighLowMedium ? Brushes.Red : Brushes.Blue;
            this.OnPropertyChanged("RiskHighLowMedium");
            this.OnPropertyChanged("Background");
        }


        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion // INotifyPropertyChanged Members
    }

XAML 代码:

<Window x:Class="backgrounChange.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
    <Style x:Key="CircleButton" TargetType="Button">

            <Setter Property="Background" Value="Blue"/>
            <Style.Triggers>
            <DataTrigger Binding="{Binding RiskHighLowMedium }"  Value="true">
                    <Setter Property="Background" Value="{ Binding Path= Background}"/>
                </DataTrigger>

            </Style.Triggers>
    </Style>
    </Window.Resources>
    <Grid>
        <Button Width="50" Height="50" Style="{DynamicResource  CircleButton}" Click="Button_Click"/>
    </Grid>
</Window>

我将不胜感激这里的任何形式的帮助。

不断更改 wpf 中按钮的背景

虽然可以更改Button.Background值,但 Button 控件的默认ControlTemplate定义了一个动画,该动画在官方Button.Background值和另一种(浅蓝色)颜色之间对Button的背景进行动画处理。要摆脱这种行为,您必须为Button定义一个新的ControlTemplate。这是一个非常基本的例子:

<Style x:Key="CircleButton" TargetType="Button">
    <Setter Property="Background" Value="Blue"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding RiskHighLowMedium}" Value="True">
            <Setter Property="Button.Background" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>
...
<Button Width="50" Height="50" Style="{StaticResource CircleButton}" 
    Click="Button_Click">
    <Button.Template>
        <ControlTemplate>
            <Border Background="{TemplateBinding Background}">
                <ContentPresenter />
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>
...
bool _highLow = false;
public bool RiskHighLowMedium
{
    get { return _highLow; }
    set { _highLow = value; OnPropertyChanged("RiskHighLowMedium"); }
}
...
private void Button_Click(object sender, RoutedEventArgs e)
{
    RiskHighLowMedium = true;
}