问题绑定渐变背景色uwp

Problem binding gradient background color uwp

我正在尝试将渐变绑定到 uwp 应用程序中的变量。 Y 尝试将其与背景绑定,但问题为零。但是当我试图将它绑定到 xaml 中的渐变时,它看起来很透明。知道为什么吗?

这里是 xaml

<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                            <GradientStop Color="{Binding Gradient0}" Offset="0" />
                                            <GradientStop Color="{Binding Gradient1}" Offset="0.210" />
                                            <GradientStop Color="{Binding Gradient2}" Offset="0.760" />
                                            <GradientStop Color="{Binding Gradient3}" Offset="0.900" />
                                        </LinearGradientBrush>

这些是我的变量:

private Brush Gradient0;
public Brush Gradient0 { get => Gradient0; set { Gradient0 = value; NotifyPropertyChanged(nameof(Gradient0)); } }

private Brush Gradient1;
public Brush Gradient1{ get => Gradient1; set { Gradient1= value; NotifyPropertyChanged(nameof(Gradient1)); } }

private Brush Gradient2;
public Brush Gradient2{ get => Gradient2; set { Gradient2 = value; NotifyPropertyChanged(nameof(Gradient2)); } }

private Brush Gradient3;
public Brush Gradient3{ get => Gradient3; set { Gradient3= value; NotifyPropertyChanged(nameof(Gradient3)); } }

我尝试使用一些渐变画笔作为平面背景颜色并且它有效,但作为渐变它没有

这是绑定了渐变的按钮的外观

这是我使用 Gradient0 值作为绑定背景颜色时的样子

Problem binding gradient background color uwp

在测试过程中,问题是Color 属性不能直接设置Brush,需要将ViewModdel的Gradient类型替换为Color class。

例如

public class HomeViewModel : INotifyPropertyChanged
{
    private Color _gradient0;
    public Color Gradient0 { get => _gradient0; set { _gradient0 = value; NotifyPropertyChanged(nameof(Gradient0)); } }

    private Color _gradient1;
    public Color Gradient1 { get => _gradient1; set { _gradient1 = value; NotifyPropertyChanged(nameof(Gradient1)); } }

    private Color _gradient2;
    public Color Gradient2 { get => _gradient2; set { _gradient2 = value; NotifyPropertyChanged(nameof(Gradient2)); } }

    private Color _gradient3;

    public Color Gradient3 { get => _gradient3; set { _gradient3 = value; NotifyPropertyChanged(nameof(Gradient3)); } }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}

乌斯佳

public HomePage()
{
    this.InitializeComponent();
    //var viewmodel = new HomeViewModel()
    //{
    //    Gradient0 = new SolidColorBrush(Colors.Red),
    //    Gradient1 = new SolidColorBrush(Colors.LightBlue),
    //    Gradient2 = new SolidColorBrush(Colors.LightCyan),
    //    Gradient3 = new SolidColorBrush(Colors.Beige)
    //};

    var viewmodel = new HomeViewModel()
    {
        Gradient0 = Colors.Red,
        Gradient1 = Colors.LightBlue,
        Gradient2 = Colors.LightCyan,
        Gradient3 = Colors.Beige
    };

    this.DataContext = viewmodel;
}