绑定背景色不起作用



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

该 xaml:

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

但这并不做任何事情,我的意思是没有背景颜色。

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}"

将背景属性更改为

private SolidColorBrush _background;
public SolidColorBrush Background
{
get
{
return _background;
}
set
{
_background = value;
OnPropertyChanged();
}
}

和改变Background = "Red"Background = new SolidColorBrush(Colors.Red);

最新更新