gpt4 book ai didi

c# - 将 SolidColorBrush 绑定(bind)到背景,绑定(bind)错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:26:13 28 4
gpt4 key购买 nike

似乎在绑定(bind)某些数据时出错了,有人可以帮我解决我哪里出错了,尽管我无法弄清楚。

真的不需要在这里显示太多,这是 Binding,我已经通过移除背景并在其中放置颜色来测试背景,效果很好。

编辑:但是绑定(bind)后没有颜色被渲染!

<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{Binding Color1}" />
</Setter.Value>
</Setter>

这是设置颜色的类,我使用 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;
}
}

显然我将我的 DataContext 设置为此类的实例,我还有其他绑定(bind),我绑定(bind)到 Message 属性并且这工作正常,所以我确定它与被绑定(bind)的数据类型。

最佳答案

您的代码尝试创建一个全新的 SolidColorBrush 对象,提供 Color1 属性值作为新 Color 属性SolidColorBrush。除了 Color1 属性本身是 SolidColorBrush 而不是 SolidColorBrush.Color 属性所需的 Color 之外。

在我看来,最明显的解决方法是直接从 Color1 属性设置 Background 属性:

<Setter Property="Background" Value="{Binding Color1}"/>

如果出于某种原因您确实需要一个全新的 SolidColorBrush 对象,则必须使用实际的 Color 值进行初始化。例如:

<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{Binding Color1.Color}" />
</Setter.Value>
</Setter>

关于c# - 将 SolidColorBrush 绑定(bind)到背景,绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33726642/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com