user9821580
user9821580

Reputation:

Binding background color doesn't work

I am trying to set background color of some icon via binding, but I'm probably missing something and don't know what.

The xaml:

<materialDesign:PackIcon x:Name="SaveIcon" Kind="ContentSave" 
                         Height="25" Width="25" Background="{Binding Background}" />

Code behind:

public Page6()
{
    InitializeComponent();
    DataContext = this;
    Background = "Red";
}

private string _background;
public string Background
{
    get
    {
        return _background;
    }

    set
    {
        _background = value;
        OnPropertyChanged();
    }
}

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName=null)
{
        PropertyChanged?.Invoke(this , new PropertyChangedEventArgs(propertyName));
}

But this don't do nothing, I mean there is no background color.

Upvotes: 0

Views: 4720

Answers (2)

ASh
ASh

Reputation: 35681

There is already Brush Background property in Control class. your string Background property hides base property, but binding Background="{Binding Background}" still picks up the base property.

You can remove string Background completely and use Brush Background, or rename your new property.

public Page6()
{
    InitializeComponent();
    DataContext = this;
    BackColor = "Red";
}

private string _background;
public string BackColor
{
        get
        {
            return _background;
        }

        set
        {
            _background = value;
            OnPropertyChanged();
        }
}

Change binding:

Background="{Binding BackColor}"

Upvotes: 1

nuuse
nuuse

Reputation: 109

change your Background property to

private SolidColorBrush _background;
public SolidColorBrush Background
{
    get
    {
        return _background;
    }

    set
    {
        _background = value;
        OnPropertyChanged();
    }
}

and change Background = "Red" to Background = new SolidColorBrush(Colors.Red);

Upvotes: 1

Related Questions