组合和依赖属性用法

时间:2018-02-22 09:13:29

标签: c# .net wpf dependency-properties

我不确定正确使用DependencyProperty.AddOwner函数。请参阅附件代码。 Type Rectangle仅用于ilustration。实际上有第三方库使用的类型。

...
using System.Windows.Shapes;
...

public class CustomShape : DependencyObject
{    
    string id;
    Rectangle rectangle;

    public CustomShape(string id)
    {
        rectangle = new Rectangle();
        this.id = id;
    }

    public static readonly DependencyProperty FillProperty = 
Rectangle.FillProperty.AddOwner(typeof(CustomShape));

    public Brush Fill
    {
        get { return (Brush)rectangle.GetValue(FillProperty); }
        set { rectangle.SetValue(FillProperty, value); }
    }                    
}

1 个答案:

答案 0 :(得分:1)

let jobs = [{ "id": 0, "json": '{"error": "Some error"}', "children": [{ "id": 1 }, { "id": 3, "children": [{ "id": 4, "json": '{"error": "Some other error"}' }] }] }, { "id": 1, "json": '{"error": "Some error"}', "children": [{ "id": 2 }, { "id": 4, "children": [{ "id": 5, "json": '{"error": "Some other error"}' }] }] }]; function parseObj(job) { let res; if (typeof job !== 'object') { return; } else { res = job.filter(elem => (elem.json && typeof elem.json == 'string')?elem.json = JSON.parse(elem.json):parseObj(elem.children)) .filter(elem => (elem.json && typeof elem.json == 'string')?elem.json = JSON.parse(elem.json):parseObj(elem.children)); } return res; } console.log(parseObj(jobs));或任何其他成员上调用set GetValue和SetValue是错误的。如果rectangle应该是CustomShape类的属性,则其声明应如下所示:

Fill

但是,您可以将Rectangle的Fill属性绑定到CustomShape:

public static readonly DependencyProperty FillProperty =
   Shape.FillProperty.AddOwner(typeof(CustomShape));

public Brush Fill
{
    get { return (Brush)GetValue(FillProperty); }
    set { SetValue(FillProperty, value); }
}

或者,您可以为Fill属性注册PropertyChangedCallback:

public CustomShape()
{
        rectangle.SetBinding(Rectangle.FillProperty,
            new Binding("Fill") { Source = this });
}