[英]Constantly changing the background of a button in wpf
我的窗戶上有一個按鈕。 默認情況下,按鈕的背景色為藍色。 單擊按鈕后,顏色應更改為紅色。 但我看到背景顏色正在變為紅色,但不是永久的。
這是課程代碼:
/// <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>
我會在這里提供任何幫助。
雖然可以更改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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.