0

我正在尝试通过绑定设置某些图标的背景颜色,但我可能遗漏了一些东西并且不知道是什么。

xml:

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

后面的代码:

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

但这不是什么都不做,我的意思是没有背景颜色。

4

2 回答 2

1

将您的背景属性更改为

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

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

并更改 Background = "Red"Background = new SolidColorBrush(Colors.Red);

于 2018-05-22T11:37:04.397 回答
1

Control 类中已有Brush Background属性。您的string Background属性隐藏Background="{Binding Background}"了基本属性,但绑定仍会获取基本属性。

您可以完全删除string Background并使用Brush Background,或重命名您的新属性。

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

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

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

更改绑定:

Background="{Binding BackColor}"
于 2018-05-22T11:44:53.580 回答