Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind a background color in WPF/XAML?

What do I have to change to the following code so that the background is red, neither of the 2 ways I tried worked:

alt text
(source: deviantsart.com)

XAML:

<Window x:Class="TestBackground88238.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Window1" Height="300" Width="300">     <StackPanel>          <TextBlock Text="{Binding Message}" Background="{Binding Background}"/>          <TextBlock Text="{Binding Message}">             <TextBlock.Background>                 <SolidColorBrush Color="{Binding Background}"/>             </TextBlock.Background>         </TextBlock>      </StackPanel> </Window> 

Code Behind:

using System.Windows; using System.ComponentModel;  namespace TestBackground88238 {     public partial class Window1 : Window, INotifyPropertyChanged     {          #region ViewModelProperty: Background         private string _background;         public string Background         {             get             {                 return _background;             }              set             {                 _background = value;                 OnPropertyChanged("Background");             }         }         #endregion          #region ViewModelProperty: Message         private string _message;         public string Message         {             get             {                 return _message;             }              set             {                 _message = value;                 OnPropertyChanged("Message");             }         }         #endregion            public Window1()         {             InitializeComponent();             DataContext = this;              Background = "Red";             Message = "This is the title, the background should be " + Background + ".";          }          #region INotifiedProperty Block         public event PropertyChangedEventHandler PropertyChanged;          protected void OnPropertyChanged(string propertyName)         {             PropertyChangedEventHandler handler = PropertyChanged;              if (handler != null)             {                 handler(this, new PropertyChangedEventArgs(propertyName));             }         }         #endregion      } } 

Update 1:

I tried Aviad's answer which didn't seem to work. I can do this manually with x:Name as shown here but I want to be able to bind the color to a INotifyPropertyChanged property, how can I do this?

alt text
(source: deviantsart.com)

XAML:

<Window x:Class="TestBackground88238.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Window1" Height="300" Width="300">     <StackPanel>          <TextBlock Text="{Binding Message}" Background="{Binding Background}"/>          <TextBlock x:Name="Message2" Text="This one is manually orange."/>      </StackPanel> </Window> 

Code Behind:

using System.Windows; using System.ComponentModel; using System.Windows.Media;  namespace TestBackground88238 {     public partial class Window1 : Window, INotifyPropertyChanged     {          #region ViewModelProperty: Background         private Brush _background;         public Brush Background         {             get             {                 return _background;             }              set             {                 _background = value;                 OnPropertyChanged("Background");             }         }         #endregion          #region ViewModelProperty: Message         private string _message;         public string Message         {             get             {                 return _message;             }              set             {                 _message = value;                 OnPropertyChanged("Message");             }         }         #endregion          public Window1()         {             InitializeComponent();             DataContext = this;              Background = new SolidColorBrush(Colors.Red);             Message = "This is the title, the background should be " + Background + ".";              Message2.Background = new SolidColorBrush(Colors.Orange);          }          #region INotifiedProperty Block         public event PropertyChangedEventHandler PropertyChanged;          protected void OnPropertyChanged(string propertyName)         {             PropertyChangedEventHandler handler = PropertyChanged;              if (handler != null)             {                 handler(this, new PropertyChangedEventArgs(propertyName));             }         }         #endregion      } } 
like image 906
Edward Tanguay Avatar asked Dec 25 '09 22:12

Edward Tanguay


People also ask

How do I change the background color in WPF?

Navigate to the Properties window, and click the Background drop-down arrow, and choose Red or another color in the color picker.


1 Answers

Important:

Make sure you're using System.Windows.Media.Brush and not System.Drawing.Brush

They're not compatible and you'll get binding errors.

The color enumeration you need to use is also different

System.Windows.Media.Colors.Aquamarine (class name is Colors) <--- use this one System.Drawing.Color.Aquamarine (class name is Color)

If in doubt use Snoop and inspect the element's background property to look for binding errors - or just look in your debug log.

like image 56
Simon_Weaver Avatar answered Oct 11 '22 00:10

Simon_Weaver