[英]Bind SolidColorBrush to Background, binding error
It seems in binding some data wrong, can someone help me out where i'm going wrong though as I can't figure it out. 似乎在绑定一些错误的数据时,有人可以帮我解决我出错的地方,尽管我无法弄明白。
Don't really need to show too much here, this is the Binding
, I have tested the background by removing the background and just putting a colour in there, that works. 不需要在这里显示太多,这是
Binding
,我已经通过删除背景测试了背景,只是在那里放了一种颜色,这是有效的。
Edit: But with the binding there is no colour being rendered! 编辑:但是绑定没有渲染颜色!
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{Binding Color1}" />
</Setter.Value>
</Setter>
This is the class where the colour is set, I use SolidColorBrush as this is what the Background property expects: 这是设置颜色的类,我使用SolidColorBrush,因为这是Background属性所期望的:
public class notificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
private string _icon;
private string _message;
private string _detail;
private SolidColorBrush _color1;
private SolidColorBrush _color2;
public string Icon
{ get { return _icon; } set { _icon = value; OnPropertyChanged("Icon"); }}
public string Message
{ get { return _message; } set { _message = value; OnPropertyChanged("Message"); } }
public string Detail
{ get { return _detail; } set { _detail = value; OnPropertyChanged("Detail"); } }
public SolidColorBrush Color1
{ get { return _color1; } set { _color1 = value; OnPropertyChanged("Color1"); } }
public SolidColorBrush Color2
{ get { return _color2; } set { _color2 = value; OnPropertyChanged("Color2"); } }
public notificationObject newNotification(int type, string message, string detail)
{
//Create new instance of object
notificationObject x = new notificationObject();
switch (type)
{
case 1:
//Fatal
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = new SolidColorBrush(Color.FromArgb(0, 170, 60, 18));
x.Color2 = new SolidColorBrush(Color.FromArgb(0, 238, 78, 16));
return x;
case 2:
//Fatal
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = new SolidColorBrush(Color.FromArgb(0, 170, 60, 18));
x.Color2 = new SolidColorBrush(Color.FromArgb(0, 238, 78, 16));
return x;
case 3:
//Unauthorized
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = new SolidColorBrush(Color.FromArgb(0, 170, 60, 18));
x.Color2 = new SolidColorBrush(Color.FromArgb(0, 238, 78, 16));
return x;
case 4:
//Warning
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = new SolidColorBrush(Color.FromArgb(0, 241, 176, 24));
x.Color2 = new SolidColorBrush(Color.FromArgb(0, 205, 152, 28));
return x;
case 5:
//Warning
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = new SolidColorBrush(Color.FromArgb(0, 41, 161, 213));
x.Color2 = new SolidColorBrush(Color.FromArgb(0, 36, 142, 184));
return x;
}
//Can't find error code
x.Icon = "";
x.Message = "Unable to find requested error code!";
x.Detail = "";
x.Color1 = new SolidColorBrush(Color.FromArgb(0, 170, 60, 18));
x.Color2 = new SolidColorBrush(Color.FromArgb(0, 238, 78, 16));
return x;
}
}
And obviously I set my DataContext to the instance of this class, I have got other bindings where I bind to the Message
property and this works fine, so i'm sure it's something to do with the data type being binded. 显然我将我的DataContext设置为此类的实例,我有其他绑定,我绑定到
Message
属性,这很好,所以我确定它与绑定的数据类型有关。
Your code attempts to create a whole new SolidColorBrush
object, providing the Color1
property value as the Color
property for the new SolidColorBrush
. 您的代码尝试创建一个全新的
SolidColorBrush
对象,将Color1
属性值作为新SolidColorBrush
的Color
属性。 Except that the Color1
property is itself a SolidColorBrush
and not a Color
as needed for the SolidColorBrush.Color
property. 除了
Color1
属性本身是SolidColorBrush
而不是SolidColorBrush.Color
属性所需的Color
。
It seems to me that the most obvious fix would be to just set the Background
property directly from the Color1
property: 在我看来,最明显的解决方法是直接从
Color1
属性设置Background
属性:
<Setter Property="Background" Value="{Binding Color1}"/>
If for some reason you really want a whole new SolidColorBrush
object, you would have to initialize using an actual Color
value. 如果由于某种原因你真的想要一个全新的
SolidColorBrush
对象,则必须使用实际的Color
值进行初始化。 For example: 例如:
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{Binding Color1.Color}" />
</Setter.Value>
</Setter>
Your Color1
property is of type SolidColorBrush
but you are binding to the Color
property of your SolidColorBrush
which has type System.Windows.Media.Color
. 您的
Color1
属性是SolidColorBrush
类型,但您绑定到SolidColorBrush
的Color
属性,其类型为System.Windows.Media.Color
。
You could bind the background property directly to the SolidColorBrush
you are creating at the moment or you could change those properties to expose a System.Windows.Media.Color
instead and keep your XAML as it is now. 您可以将background属性直接绑定到您正在创建的
SolidColorBrush
,或者您可以更改这些属性以显示System.Windows.Media.Color
并保持您的XAML现在。
Note, since you have setters to change those colours, that you'll need to implement INotifyPropertyChanged
if you want any changes to be reflected in the view. 请注意,由于您有更改这些颜色的setter,因此如果您希望在视图中反映任何更改,则需要实现
INotifyPropertyChanged
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.