WPF继承了属性更改通知

时间:2016-12-16 08:20:35

标签: c# wpf xaml custom-controls attached-properties

如何更改继承属性以更新自定义控件?

我已将视图模型file = open('path/to/file.xlsx', 'rb') wb = openpyxl.load_workbook(filename=file) 定义为属性Circle

Radius

现在控件本身带有几个属性:public class Circle : Freezable { public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register("Radius", typeof(double), typeof(Circle), new FrameworkPropertyMetadata(10.0)); public double Radius { get { return (double)GetValue(RadiusProperty); } set { SetValue(RadiusProperty, value); } } protected override Freezable CreateInstanceCore() => new Circle(); } Direct)和AffectsRenderInherited)类型AffectsRender|Inherits

Circle

现在的问题。这样:

class View : FrameworkElement
{
    // Inherited

    public static readonly DependencyProperty InheritedProperty =
        DependencyProperty.RegisterAttached(
            "Inherited", typeof(Circle), typeof(View),
                new FrameworkPropertyMetadata(
                    null, 
                    FrameworkPropertyMetadataOptions.AffectsRender | 
                    FrameworkPropertyMetadataOptions.Inherits));

    public static void SetInherited(UIElement element, Circle value)
    {
        element.SetValue(InheritedProperty, value);
    }

    public static Circle GetInherited(UIElement element)
    {
        return (Circle)element.GetValue(InheritedProperty);
    }

    public Circle Inherited
    {
        get { return GetInherited(this); }
        set { SetInherited(this, value); }
    }

    // Direct

    public static readonly DependencyProperty DirectProperty = 
        DependencyProperty.RegisterAttached(
            "Direct", typeof(Circle), typeof(View),
                new FrameworkPropertyMetadata(
                    null, 
                    FrameworkPropertyMetadataOptions.AffectsRender));

    public static void SetDirect(UIElement element, Circle value)
    {
        element.SetValue(DirectProperty, value);
    }

    public static Circle GetDirect(UIElement element)
    {
        return (Circle)element.GetValue(DirectProperty);
    }

    public Circle Direct
    {
        get { return GetDirect(this); }
        set { SetDirect(this, value); }
    }

    // Render

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        drawingContext.DrawEllipse(Brushes.Black, null, 
            new Point(), Inherited.Radius, Direct.Radius);
    }
}

以下代码重新呈现视图:

<local:View x:Name="_view">
    <local:View.Inherited>
        <local:Circle/>
    </local:View.Inherited>
    <local:View.Direct>
        <local:Circle/>
    </local:View.Direct>
</local:View>

但是当XAML看起来像这样:

    private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _view.Inherited.Radius+=10;
    }

    private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        _view.Direct.Radius++;
    }

此行停止更新屏幕:

<local:View.Inherited>
    <local:Circle/>
</local:View.Inherited>
<local:View x:Name="_view">
    <local:View.Direct>
        <local:Circle/>
    </local:View.Direct>
</local:View>

我做错了什么?

0 个答案:

没有答案