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

问题描述 投票:0回答:1

我使用一个显示不同测试的列表框,当测试正常时我想以绿色显示测试,当测试不正常时以红色显示测试,我使用数据绑定来显示测试列表并绑定颜色。

我可以显示测试结果,但我无法用我的方法为每个项目着色,程序将所有项目(“所有测试”)设置为绿色或将所有测试设置为红色,但我正在寻找的是两个具有不同的前景我的列表框的项目(测试)取决于测试结果:

我的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

感谢您的反馈,如果您需要更多信息,请不要犹豫:
全局的想法是看到不同的测试以不同的颜色运行=>有可能为列表框的每个项目提供我们可以通过后面的代码选择的颜色

wpf data-binding listbox foreground itemssource
1个回答
0
投票

使用数据模板:

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

您可能希望将

ColorStatus
属性移动到处理 ListView Item 的视图模型类。

© www.soinside.com 2019 - 2024. All rights reserved.