我正在尝试绑定背景颜色,但由于某种原因它没有更新控件,我可以看到它击中了属性的获取,但它没有更新 GUI。有什么我想念的吗?
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
// ...
private Color m_myColorProperty;
public Color MyColorProperty
{
get
{
return m_myColorProperty;
}
set
{
m_myColorProperty = value;
OnPropertyChanged("MyColorProperty");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
和 xaml:
<Window x:Class="TestApp.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">
<Grid>
<Grid.Background>
<SolidColorBrush Color="{Binding MyColorProperty}"/>
</Grid.Background>