【问题标题】:WPF Binding Color PropertyWPF 绑定颜色属性
【发布时间】:2021-11-10 11:12:26
【问题描述】:

我对 WPF 非常陌生,并且正在研究一些示例控件并尝试调整它们以熟悉事情的工作方式。 我目前的任务是下面有一个 LinearGradientBrush 代码

<LinearGradientBrush x:Key="ThumbBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientStop Offset="0" Color="#c1dbe8"/>
        <GradientStop Offset="0.5" Color="#008fc7"/>
        <GradientStop Offset="1" Color="#066caa"/>
    </LinearGradientBrush>

我想将其转换为属性,以便可以修改颜色。我的想法是我可以简单地为每种颜色创建一个属性

public Color MyThumbColor1 { get; set; }
public Color MyThumbColor2 { get; set; }
public Color MyThumbColor3 { get; set; }

然后将每种颜色绑定到各自的 GradientStop

<GradientStop Offset="0" Color="{Binding MyThumbColor1}"/>
<GradientStop Offset="0.5" Color="{Binding MyThumbColor1}"/>
<GradientStop Offset="1" Color="{Binding MyThumbColor1}"/>

这不起作用,因此非常感谢有关如何绑定颜色属性的任何指导

【问题讨论】:

  • 当前 DataContext 是什么 - 即用作绑定源对象的对象?源属性设置在哪里?
  • 要创建绑定,除了属性之外,您还必须指定源对象。默认情况下(即,如果未指定)它使用来自 DataContext 的对象。此外,在声明属性的类中,为了使绑定在更改时自动更新,必须实现 INPC 接口并通过它通知属性值的更改。显示更多实施细节以获得更具体的答案。

标签: wpf xaml colors binding


【解决方案1】:

我好像忘了包含

this.DataContext = this;

就在 InitializeComponent(); 之后在 NewControl.xaml.cs 文件上

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    相关资源
    最近更新 更多