我使用一个列表框来显示不同的测试,当测试正常时,我想用绿色来显示测试,当测试不正常时,我想用红色来显示测试,我使用数据绑定来显示测试列表,也使用绑定来显示颜色。
我可以显示测试结果,但我不能用我的方法为每个项目着色程序将所有项目(“所有测试“)着色为绿色或红色,但我要找的是两个具有不同前景的项目(测试)我的列表框取决于测试结果:
我的Xaml:
<ListBox x:Name="myListBox" ItemsSource="{Binding MaListe}" Foreground="{Binding ColorStatus}" ScrollViewer.VerticalScrollBarVisibility="Visible"
// Code behind :
// here to bind the color foreground with varriable : ColorStatus
public string _colorStatus;
public string ColorStatus
{
get { return _colorStatus; }
set { _colorStatus = value; OnPropertyChanged(nameof(ColorStatus)); }
}
// to bind itemsSource with variable _maList that contains my string of characters " my Tests"
public ObservableCollection<string> _maListe;
public ObservableCollection<string> MaListe
{
get { return _maListe; }
set { _maListe = value; }
}
// Mylist of caractère that I constract to show :
public ObservableCollection<string> Getlist()
{
DateTime DateNow = DateTime.Now;
string DateTimeNow = "DateTime : " + DateTime.Now.ToString("");
int year = DateNow.Year;
int month = DateNow.Month;
int day = DateNow.Day;
int hour = DateNow.Hour;
int minute = DateNow.Minute;
int second = DateNow.Second;
int millisecond = DateNow.Millisecond;
Results.Add(" * " + DateTimeNow + " : " +
TestName + " " +
ExpectedValue + " : " + " [ " +
MinValue + " , " +
MaxValue + " ]" + " >>> " +
Status);
return Results;
}
}
// Declaration
ResultListContainer ResultList = new ResultListContainer();
// Here a small trial :
ResultList.TestName = " Contact Test ";
ResultList.ExpectedValue = "12 V";
ResultList.MinValue = "11 V";
ResultList.MaxValue = "13 V";
ResultList.Status = "OK";
_maListe = ResultList.Getlist();
if (ResultList.Status == "OK")
ColorStatus = "#FFC4FD03"; // Green color
else
ColorStatus = "#FFFF4500"; // Red Color
ResultList.TestName = " Leakage Test";
ResultList.ExpectedValue = "105 mA";
ResultList.MinValue = "110 V";
ResultList.MaxValue = "100 V";
ResultList.Status = "NOK";
if (ResultList.Status == "OK")
ColorStatus = "#FFC4FD03"; // Green color
else
ColorStatus = "#FFFF4500"; // Red Color
_maListe = ResultList.Getlist();
// here the result :
enter image description here
感谢您反馈,如果您需要更多信息,请不要犹豫:
全局的想法是看到不同的测试运行不同的颜色=>有可能给给予列表框的每一个项目的颜色,我们可以选择的代码后面
1条答案
按热度按时间eqqqjvef1#
使用数据模板:
您可能希望将
ColorStatus
属性移动到处理ListView项的视图模型类。MaListe
集合中单个项的类型。