Afnan Bashir
Afnan Bashir

Reputation: 7419

Bind label Foreground to a variable

I have a User Interface and I am changing the background property of the main grid. Now some give a very pleasent look but for some there is difficulty reading the text as displayed. However, a problem arises when I have approximately 20 labels around there now, and making them change and assign them color each time is making my code look ugly. I know there must be a more elegant design.

I tried to bind labels to a color but that does not work. here is code

XAML:

<Label Foreground="{Binding defColor}" Content="Settings" Height="44" HorizontalAlignment="Left" Margin="12,53,0,0" Name="label1" VerticalAlignment="Top" FontWeight="Normal" FontSize="26" />

Code behind:

  SolidColorBrush defColor = new SolidColorBrush();

  public SettingsWindow()
  {
    InitializeComponent();
    defColor.Color = Colors.Black;
    //defColor.Color = Colors.Black;  label1.Foreground = defColor;
  }

  private void button4_Click(object sender, RoutedEventArgs e)
  {
   defColor.Color = Colors.Black;
  }

Thanks

Upvotes: 0

Views: 1529

Answers (3)

Steven Licht
Steven Licht

Reputation: 770

If you are setting the SettingsWindow DataContext = this; then the SettingsWindow class MUST implement INotifyPropertyChanged. to get the {Binding defColor} to work. Code you need:

public class SettingsWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public SettingsWindow()
    {
        // We are acting as our own 'ViewModel'
        DataContext = this;
        InitializeComponent();
    }

    private Color _defColor;
    public Color defColor
    {
        get { return _defColor; }
        set
        {
            if (_defColor != value)
            {
                _defColor = value;
                if(null != PropertyChanged)
                {
                    PropertyChanged(this, "defColor");
                }
            }
        }
    }
}

As for targeting all labels in your application, the correct approach is to use Style, as previously suggested. You will have to apply this style to each Label. Omit the x:Key to make it default style for your Labels

    <Style x:Key="LabelForeGroundStyle" TargetType="{x:Type Label}">
         <Setter Property="Foreground" Value="{Binding defColor}" />
    </Style> 

Upvotes: 1

Amittai Shapira
Amittai Shapira

Reputation: 3827

Instead of Binding each label to the same property, I think you should use style, and apply this style for each label, e.g. with your binding:

  <Style x:Key="LabelForeGroundStyle" TargetType="{x:Type Label}">
    <Setter Property="Foreground" Value="{Binding defColor}" />
  </Style>

or even better, with trigger:

<Style.Triggers>
 <Trigger>
   <Trigger Property="Background" Value="Blue">
     <Setter Property="Foreground" Value="Green"/>
   </Trigger>
 </Trigger>
</Style.Triggers>

Upvotes: 1

Malcolm O&#39;Hare
Malcolm O&#39;Hare

Reputation: 4999

Just from looking at the C# code you posted, I think your first problem is that you've done this

SolidColorBrush defColor = new SolidColorBrush(); 

instead of this

public SolidColoRBrush defColor { get; set; }

You can only bind to properties.

Your constructor will now look like this

public SettingsWindow()  
{  
    InitializeComponent();  
    defColor = new SolidColorBrush(Colors.Black);
    this.DataContext = this;  // need to tell your window where to look for binding targets
}  

Upvotes: 2

Related Questions