Edw*_*uay 31 c# wpf xaml binding background
我需要更改为以下代码,以便背景为红色,我尝试的两种方式都没有:
替代文字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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我试过Aviad的答案似乎不起作用.我可以使用x:Name手动执行此操作,如此处所示,但我希望能够将颜色绑定到INotifyPropertyChanged属性,我该怎么做?
替代文字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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
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
}
}
Run Code Online (Sandbox Code Playgroud)
Sim*_*ver 53
重要:
确保你正在使用System.Windows.Media.Brush
而不是System.Drawing.Brush
它们不兼容,你会遇到绑定错误.
您需要使用的颜色枚举也不同
System.Windows.Media.Colors.Aquamarine(类名是Colors
)<---使用这一个System.Drawing.Color.Aquamarine(类名是Color
)
如果有疑问,请使用Snoop
并检查元素的background属性以查找绑定错误 - 或者只查看调试日志.
Avi*_* P. 22
该Background
属性需要一个Brush
对象,而不是一个字符串.将属性的类型更改为Brush
并初始化它:
Background = new SolidColorBrush(Colors.Red);
Run Code Online (Sandbox Code Playgroud)
在这里你有一个复制粘贴代码:
class NameToBackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value.ToString() == "System")
{
return new SolidColorBrush(System.Windows.Media.Colors.Aqua);
}else
{
return new SolidColorBrush(System.Windows.Media.Colors.Blue);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
107863 次 |
最近记录: |