Reputation: 11
I am trying to bind a background color but for some reason it is not updating the control, I can see it hitting the get of the property but it doesn't update the GUI. Is there something I am missing?
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));
}
}
}
And the 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>
Upvotes: 1
Views: 2309
Reputation: 25146
since you're using "this" (a window) as the datacontext, would it be simpler to use a DependencyProperty
instead of going through the work of implementing INotifyPropertyChanged
?
Upvotes: 0
Reputation: 10410
You should bind a Brush type not a color. SolidBrush, Gradient Brush, etc. if you want a single color use solid brush
Upvotes: 1
Reputation:
Using your code as a basis, adding the following inside of the MainWindow
constructor provided me with a Window sporting a lovely pink background:
this.MyColorProperty = (Color)ColorConverter.ConvertFromString("#FFCC0099");
Upvotes: 0