使用数据绑定WPF的每个项目的列表框颜色

ukqbszuj  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(103)

我使用一个列表框来显示不同的测试,当测试正常时,我想用绿色来显示测试,当测试不正常时,我想用红色来显示测试,我使用数据绑定来显示测试列表,也使用绑定来显示颜色。
我可以显示测试结果,但我不能用我的方法为每个项目着色程序将所有项目(“所有测试“)着色为绿色或红色,但我要找的是两个具有不同前景的项目(测试)我的列表框取决于测试结果:
我的Xaml:

<ListBox   x:Name="myListBox"  ItemsSource="{Binding MaListe}" Foreground="{Binding ColorStatus}"  ScrollViewer.VerticalScrollBarVisibility="Visible"

enter image description here

// 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
感谢您反馈,如果您需要更多信息,请不要犹豫:
全局的想法是看到不同的测试运行不同的颜色=>有可能给给予列表框的每一个项目的颜色,我们可以选择的代码后面

eqqqjvef

eqqqjvef1#

使用数据模板:

<ListBox   x:Name="myListBox"  ItemsSource="{Binding MaListe}"   ScrollViewer.VerticalScrollBarVisibility="Visible" >
   <ListBox.ItemTemplate>
      <DataTemplate>
          <TextBlock Foreground="{Binding ColorStatus}" ... />
      </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

您可能希望将ColorStatus属性移动到处理ListView项的视图模型类。MaListe集合中单个项的类型。

相关问题