title: I want to change button background color dynamically in based on collection using Wpf?
body: <p>Student:</p>
  
  <pre><code>public class Student
  {
      private int _studentNo;
      public int StudentNo
      {
          get { return _studentNo; }
          set { _studentNo = value; }
      }
  
      private Rank _state;
      public Rank State
      {
          get { return _state; }
          set { _state = value; }
      }
  }
  </code></pre>
  
  <p>Rank:</p>
  
  <pre><code>public enum Rank
  {
      Pass,
      Fail
  }
  </code></pre>
  
  <p>RankBoard:</p>
  
  <pre><code>public class RankBoard
  {
  
      private static ObservableCollection&lt;Student&gt; _studentList;
      public static ObservableCollection&lt;Student&gt; StudentList
      {
          get { return _studentList; }
          set { _studentList = value; }
      }
  
      public RankBoard()
      {
  
          LoadDetails();
      }
  
      private void LoadDetails()
      {
          StudentList = new ObservableCollection&lt;Student&gt;()
          {
              new Student()
              {
                  StudentNo=1,
                  State=Rank.Pass
              },
              new Student()
              {
                  StudentNo=2,
                  State=Rank.Fail
              },
              new Student()
              {
                  StudentNo=3,
                  State=Rank.Pass
              },
          };
      }
  }
  </code></pre>
  
  <p>BackgroundChangedConvertor:</p>
  
  <pre><code>public class BackgroundChange : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          var b = value as Button;
          if(b!=null)
          {
              foreach (var x in RankBoard.StudentList)
              {
                  if ((System.Convert.ToInt32(b.Content)==x.StudentNo)&amp;&amp;((x.State.ToString()=="Pass")))
                  {
                      return new SolidColorBrush(Colors.Green);
                  }
                  else
                  {
                      return new SolidColorBrush(Colors.Red);
                  }
              }
          }
          return null;
  
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>   &lt;Window x:Class="ButtonColorChanged.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:ButtonColorChanged"
      mc:Ignorable="d"
      Title="MainWindow" Height="350" Width="525"&gt;
  
  &lt;Window.Resources&gt;
      &lt;local:BackgroundChange x:Key="Color"/&gt;
  &lt;/Window.Resources&gt;
  
  &lt;Grid&gt;
      &lt;StackPanel VerticalAlignment="Center"&gt;
          &lt;Button x:Name="btn1" Content="1" Width="75" Height="25" Background="{Binding Converter={StaticResource Color}}"/&gt;
          &lt;Button x:Name="btn2" Content="2" Width="75" Height="25" Margin="0 20 0 20" Background="{Binding Converter={StaticResource Color}}"/&gt;
          &lt;Button x:Name="btn3" Content="3" Width="75" Height="25" Background="{Binding Converter={StaticResource Color}}"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>I tried to change button background color depends upon to State(Pass or Fail) in Collection. but i Run this progrom object value throw null value in BackgroundChange class. so Button background color doesn't affect.? How do i do it.? </p>