将颜色绑定到Solidbrush.在c#代码中着色



我有一个类Cell看起来像这样:

public Color color{get { return colorr; }
set { colorr = value;
        if (this.PropertyChanged != null){
            this.PropertyChanged(this, new PropertyChangedEventArgs("color"));
        }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

我添加了很多"单元格"的viewport3D生成立方体。细胞的颜色会随着时间的推移而改变。所以我的问题是-而不是重新绘制单元格每次他们改变,我可以绑定一个单元格的颜色到solidbrush的代码?

我有这样的东西,但是它不起作用。

Binding b = new Binding();
        b.Source = cell.color;
        SolidColorBrush solidBrush = new SolidColorBrush();
        BindingOperations.SetBinding(solidBrush, SolidColorBrush.ColorProperty, b);
        Material material = new DiffuseMaterial(solidBrush);

我现在假设,当单元格的颜色改变时,solidBrush的颜色也会改变,因此viewport3D上立方体的颜色也会改变。

谢谢——大卫

请阅读数据绑定概述以及如何调试绑定。

Source的更改没有绑定更新,如果你想要更新,你需要设置一个相对于源的Path。例如

b.Source = cell;
b.Path = new PropertyPath("color");

这是因为绑定将订阅源上的INPC(和路径上的非叶子),并检查事件报告的名称是否与路径匹配,如果匹配,则更新目标。

最新更新