Change foreground color in XAML Binding (not working)

Asked

Viewed 30 times

1

I have a text that color should alternate according to the method check below:

public Color VerificaAlarmes
            {
                get => ChecarLimites? AlarmColors: Color.Gold;
            }

            public Color AlarmColors
            {
                get
                {
                    if (Value > AltoAlm)
                        return Color.Red;
                    else if (Value < BaixoAlm)
                        return Color.Green;
                    else
                        return Color.Gold;
                }
            }

This is the Binding in the text in XAML

<TextBlock Text="{Binding Descrição}"
                                           Foreground="{Binding VerificaAlarmes}" 
                                           FontSize="18"
                                           FontWeight="Semibold" />

I put the Color being type System.Drawing.Color only that the colors were not returned. Then I put the color being System.Windows.Media.Brushes and it didn’t work either, the returns of colors still don’t happen.

1 answer

1

It is likely that the value is being changed, but due to the lack of notification, the UI is not being updated, try to implement the interface Inotifypropertychanged.

Follow an example:

Mainwindow.xaml

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        x:Name="thisWindow"
        DataContext="{x:Reference thisWindow}"
        Title="MainWindow" Height="450" Width="800">
    <DockPanel>

        <TextBox Text="{Binding Valor, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Top" Margin="10"  />

        <Rectangle Fill="{Binding VerificaAlarmes}"  Margin="10" />

    </DockPanel>
</Window>

Mainwindow.xaml.Cs

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private int _valor = 50;

    public int Valor
    {
        get => _valor;
        set
        {
            if (value == _valor) return;
            _valor = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(VerificaAlarmes));
        }
    }

    public Brush VerificaAlarmes => AlarmColors;

    public Brush AlarmColors
    {
        get
        {
            if (Valor > 75)
                return Brushes.Red;
            else if (Valor < 25)
                return Brushes.Green;
            else
                return Brushes.Gold;
        }
    }

    public MainWindow()
    {
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.