0

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.

4
  • 2
    I think this is because Background property is of type Brush, not string, but I can't test it right now Commented May 22, 2018 at 11:17
  • 1
    You should see binding errors in Output window during debugging. Commented May 22, 2018 at 11:19
  • there is no binding errors Commented May 22, 2018 at 11:26
  • plz post answers Commented May 22, 2018 at 11:31

2 Answers 2

1

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}"
Sign up to request clarification or add additional context in comments.

1 Comment

@PabloEscobar: Did you really bind to "BackColor"?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.