title: Silverlight - Changing button text color created dynamically
body: <p>I am trying to change a button text color of dynamically created buttons on a Silverlight form using ViewModel. The problem I face is, when I change the color of a button text, all the buttons will get effected. Since, the buttons are dynamically created, I can't have a control on it. </p>
  
  <p>I was suggested to write a ForegroundColor property into Model and then attach to the button, I tried as you see in code, but couldn't do much about it.</p>
  
  <p>Could you see as what I am doing and help me with your suggestions as I am not sure, I am doing it right way.</p>
  
  <p>Thanks</p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>namespace Web.Models
  {
    [DataContract(IsReference = true)]
    public class Sales
    {
      [DataMember]
      public int SalesId { get; set; }
      [DataMember]
      public int ShowOrder { get; set; }
      [DataMember]
      public bool Active { get; set; }
      [DataMember]
      public bool Regurgitate { get; set; }
  
      [DataMember]
      public int ForegroundColor { get; set; }
  
      public Sales(Salese result)
      {
          SalesId = result.SalesId;
          ShowOrder = result.ShowOrder;
          Active = result.Active;
          Regurgitate = result.Regurgitate;            
  
          if (SalesId == 12)
          {
              var bytes = System.BitConverter.GetBytes(ForegroundColor);
              Color btnColor = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);
  
              SolidColorBrush myBrush = new SolidColorBrush(btnColor);
          }
     }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>private Brush _foregroundColor = new SolidColorBrush(Colors.Black); 
  
  public override void Loaded()
  {
      OnMainOutcome();
  }
  
  
  public Brush ForegroundColor
  {
     get { return _foregroundColor; }
     set
     {
         if (_foregroundColor == value) return;
         _foregroundColor = value;                
  
         OnPropertyChanged("ForegroundColor");
      }
   }
  
   private void OnMainOutcome()
   {
      var selectedSalesId = (int)OutcomeCommand.CommandParameter;
      CurrentSubOutcomes = GetCurrentSubOutcomes(selectedSalesId);
  
      foreach (var index in CurrentOutcomes)
      {
         if (index.OutcomeId == 12)        
            ForegroundColor = new SolidColorBrush(Colors.Red);
         else
            ForegroundColor = new SolidColorBrush(Colors.Black);
      }
   }
  </code></pre>
  
  <p><strong>XAML Edited</strong></p>
  
  <pre><code> &lt;controls:ChildWindow.Resources&gt;
      &lt;converters:NumericToColorConverter x:Key="NumericToColorConverter"/&gt;
  &lt;/controls:ChildWindow.Resources&gt;
  
   &lt;ListBox Grid.Row="1" Height="Auto" MinHeight="200" Width="160" Margin="2,2,2,2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Path=CurrentOutcomes}" Background="{x:Null}" BorderBrush="{x:Null}"&gt;
        &lt;ListBox.ItemTemplate&gt;
            &lt;DataTemplate&gt;
                 &lt;Button Height="30" Width="150" HorizontalAlignment="Center" Content="{Binding Outcome}" CommandParameter="{Binding SalesOutcomeId }" Command="{Binding Source={StaticResource ViewModel}, Path=OutcomeCommand}" Foreground="{Binding Source={StaticResource ViewModel}, Converter={StaticResource NumericToColorConverter}, Path=ForegroundColor}" /&gt;
            &lt;/DataTemplate&gt;
        &lt;/ListBox.ItemTemplate&gt;
   &lt;/ListBox&gt;
  </code></pre>
  
  <p><strong>Converter Class NEW</strong></p>
  
  <pre><code>  using System;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows;
  
    namespace Converters
    {
        public class NumericToColorConverter : IValueConverter
        {
          static readonly SolidColorBrush RED_BRUSH = new SolidColorBrush(Colors.Red);
          static readonly SolidColorBrush BLUE_BRUSH = new SolidColorBrush(Colors.Blue);
  
          public object Convert(object value, Type targetType,
          object parameter, System.Globalization.CultureInfo culture)
          {
             //Int32 id = System.Convert.ToInt32(value);
  
             //LinearGradientBrush brush = new LinearGradientBrush();
             //brush.StartPoint = new Point(0, 1);
             //brush.EndPoint = new Point(0, 0);
             //brush.GradientStops.Add(new GradientStop()
             //{
             //    Color = Colors.White,
             //    Offset = 0
             //});
             //brush.GradientStops.Add(new GradientStop()
             //{            
             //    Color = Color.FromArgb(
             //        200,
             //        System.Convert.ToByte((id * 103) % 256),
             //        System.Convert.ToByte((id * 157) % 256),
             //        System.Convert.ToByte((id * 233) % 256)
             //    ),
             //    Offset = 1
             //});
  
             //return brush;
             var OutcomeId = (int)value;
  
             if (OutcomeId == 12)
             {
                return RED_BRUSH;
             }
             else
             {
                return BLUE_BRUSH;
             }
      }
  
      public object ConvertBack(object value, Type targetType, object parameter,
          System.Globalization.CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>}</p>