在 C# 代码中将颜色绑定到 Solidbrush.color

发布于 2024-12-20 20:40:44 字数 842 浏览 1 评论 0原文

我有一个 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 中以生成立方体。细胞的颜色会随着时间的推移而变化。所以我的问题是 - 我可以将单元格的颜色绑定到代码中的实体画笔,而不是每次更改时都重新绘制单元格吗?

我有类似的东西,但它不起作用。

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 上立方体的颜色也会改变。但事实并非如此。

谢谢 - 大卫

I have a class Cell looking something like this:

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

Im adding a lot of "cell"'s to a viewport3D for generating cubes. And the colors of the cells change as time goes. So my question is - instead of redrawing the cells everytime they change, can I bind a cell's color to a solidbrush in code?

I have something like this, but it wont work.

Binding b = new Binding();
        b.Source = cell.color;

        SolidColorBrush solidBrush = new SolidColorBrush();
        BindingOperations.SetBinding(solidBrush, SolidColorBrush.ColorProperty, b);

        Material material = new DiffuseMaterial(solidBrush);

I would suppose now that the color of the solidBrush would change when the cell's color changes, and therefore the color of the cube on the viewport3D changes. But it doesnt.

Thanks
- David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2024-12-27 20:40:44

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

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

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

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

Please read the data binding overview and how to debug bindings.

There are no binding updates to changes of the Source, if you want updates you need to set a Path relative to a source. e.g.

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

This is because the binding will subscribe to INPC on the source (and non-leaves on the path) and check if the name reported by the event matches the path, if so the target is updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文