Wie kann ich eine Hintergrundfarbe in WPF / XAML binden?
Was muss ich in den folgenden Code ändern, damit der Hintergrund rot ist, keine der beiden Möglichkeiten, die ich ausprobiert habe:
Alternativtext http://www.deviantsart.com/upload/1okq25l.png
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 hinter:
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:Ich versuchte es mit Aviads Antwort, die anscheinend nicht funktionierte. Ich kann dies manuell mit x: Name tun, wie hier gezeigt, aber ich möchte die Farbe an eine INotifyPropertyChanged-Eigenschaft binden können. Wie kann ich das tun?
Alternativtext http://www.deviantsart.com/upload/7tp48m.png
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 hinter:
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
}
}