Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:10:16+00:00 2026-06-17T13:10:16+00:00

I am trying to change a button text color of dynamically created buttons on

  • 0

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.

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.

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.

Thanks

Model

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);
        }
   }
}

}

ViewModel

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);
    }
 }

XAML Edited

 <controls:ChildWindow.Resources>
    <converters:NumericToColorConverter x:Key="NumericToColorConverter"/>
</controls:ChildWindow.Resources>

 <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}">
      <ListBox.ItemTemplate>
          <DataTemplate>
               <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}" />
          </DataTemplate>
      </ListBox.ItemTemplate>
 </ListBox>

Converter Class NEW

  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();
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:10:17+00:00Added an answer on June 17, 2026 at 1:10 pm

    You bind the Foreground to a value on a StaticResource so all buttons will bind to this same value.

    You can either create a specific “ButtonForegroundConverter” or you can add the Brush property to the item level viewmodel that also has the Outcome property which you already bind to the Content of the button. Then the button xaml will look something like this:

    <Button ...
            Content="{Binding Outcome}" 
            Foreground="{Binding OutcomeForegroundBrush}" 
            ...
            />
    

    If you are binding directly to an entity, then adding properties like that would not be a good idea, so the example above assumes you have a intermediate viewmodel or controller on which you could add properties like that.

    If you choose to use a converter it would look something like this:

    <Button ...
            Content="{Binding Outcome}" 
            Foreground="{Binding OutcomeId, Converter={StaticResource OutcomeToForegroundConverter}}" 
            ...
            />
    

    And the converter:

    namespace MyConverters {
    
        public sealed class OutcomeToColorConverter : IValueConverter {
    
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    
                // We must have a valid integer value, double check bindings
                if (value == null) {
                    throw new ArgumentNullException("value", 
                        "Please make sure the value is not null.");
                }
    
                var OutcomeId = (int)value;
    
                if (OutcomeId == XXX) { 
                    return RED_BRUSH; 
                }
                else {
                    return BLUE_BRUSH;
                }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
                throw new NotImplementedException();
            }
    
            static readonly SolidColorBrush RED_BRUSH = new SolidColorBrush(Colors.Red);
    
            static readonly SolidColorBrush BLUE_BRUSH = new SolidColorBrush(Colors.Blue);
        }
    }
    

    Remember to declare the resource:

    <myconverters:OutcomeToForegroundConverter x:Key="OutcomeToForegroundConverter" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to change the text color of a button(api: Button) dynamically, but
I am trying to change the button size dynamically, but after using setWidth() and
I am trying to change the checked attribute of dynamically created html radio button
i'm trying to change the Text on the BUTTON dynamically which is in the
I am trying to change the text color on the selected radio button's text
I am trying to change the color of the text inside the button on
I'm trying to change the font color of the text on my back button
I am trying to do change the color of a disabled button's border/text. Code:
I am trying to change the color of the text on the buttons. Here
I am trying to change my ipad apps button title programatically using the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.