我怎样才能绑定在WPF / XAML背景色? [英] How can I bind a background color in WPF/XAML?
问题描述
我有什么改变以下code,使背景是红色的,既不的2种方法我试过的工作:
XAML:
<窗口x:类=TestBackground88238.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300WIDTH =300>
<&StackPanel的GT; < TextBlock的文本={绑定消息}后台={结合背景}/> < TextBlock的文本={绑定消息}>
< TextBlock.Background>
<的SolidColorBrush颜色={结合背景}/>
< /TextBlock.Background>
< / TextBlock的> < / StackPanel的>
< /窗GT;
code背后:
使用System.Windows;
使用System.ComponentModel;命名空间TestBackground88238
{
公共部分类窗口1:窗口,INotifyPropertyChanged的
{ #地区ViewModelProperty:背景
私人字符串_background;
公共字符串背景
{
得到
{
返回_background;
} 组
{
_background =价值;
OnPropertyChanged(背景);
}
}
#endregion #地区ViewModelProperty:消息
私人字符串_message;
公共字符串消息
{
得到
{
返回_message;
} 组
{
_message =价值;
OnPropertyChanged(信息);
}
}
#endregion 公共窗口1()
{
的InitializeComponent();
的DataContext =这一点; 背景=红;
消息=这是标题,背景应该是+背景+。; } #区域INotifiedProperty座
公共事件PropertyChangedEventHandler的PropertyChanged; 保护无效OnPropertyChanged(字符串propertyName的)
{
PropertyChangedEventHandler处理器=的PropertyChanged; 如果(处理!= NULL)
{
处理器(这一点,新PropertyChangedEventArgs(propertyName的));
}
}
#endregion }
}
更新1:
我试过阿维亚德的回答这似乎不工作。我可以用X手动执行此操作:名称如下图所示,但我希望能够以颜色绑定到INotifyPropertyChanged的财产,我怎么能做到这一点。
XAML:
<窗口x:类=TestBackground88238.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300WIDTH =300>
<&StackPanel的GT; < TextBlock的文本={绑定消息}后台={结合背景}/> < TextBlock的X:名称=消息2文本= />中这一个是手动的橙色。 < / StackPanel的>
< /窗GT;
code背后:
使用System.Windows;
使用System.ComponentModel;
使用System.Windows.Media;命名空间TestBackground88238
{
公共部分类窗口1:窗口,INotifyPropertyChanged的
{ #地区ViewModelProperty:背景
私人刷_background;
公共刷背景
{
得到
{
返回_background;
} 组
{
_background =价值;
OnPropertyChanged(背景);
}
}
#endregion #地区ViewModelProperty:消息
私人字符串_message;
公共字符串消息
{
得到
{
返回_message;
} 组
{
_message =价值;
OnPropertyChanged(信息);
}
}
#endregion 公共窗口1()
{
的InitializeComponent();
的DataContext =这一点; 背景=新的SolidColorBrush(Colors.Red);
消息=这是标题,背景应该是+背景+。; Message2.Background =新的SolidColorBrush(Colors.Orange); } #区域INotifiedProperty座
公共事件PropertyChangedEventHandler的PropertyChanged; 保护无效OnPropertyChanged(字符串propertyName的)
{
PropertyChangedEventHandler处理器=的PropertyChanged; 如果(处理!= NULL)
{
处理器(这一点,新PropertyChangedEventArgs(propertyName的));
}
}
#endregion }
}
重要:确保你使用 System.Windows.Media.Brush
而不是 System.Drawing.Brush
他们不兼容,你就会得到绑定错误。对于彩色蓝宝石:
System.Windows.Media.Colors.Aquamarine(颜色
)
System.Drawing.Color.Aquamarine(颜色
)
如果有疑问使用史努比
并检查元素的background属性,并查找绑定错误 - 或者只是在你的调试日志
What do I have to change to the following code so that the background is red, neither of the 2 ways I tried worked:
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?
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
}
}
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. For the color Aquamarine:
System.Windows.Media.Colors.Aquamarine (Colors
)
System.Drawing.Color.Aquamarine (Color
)
If in doubt use Snoop
and inspect the element's background property and look for binding errors - or just in your debug log.
这篇关于我怎样才能绑定在WPF / XAML背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!