属性设置器在XAML中不起作用

时间:2017-02-16 06:15:58

标签: c# wpf xaml

自定义控件从Canvas延伸,并且只能接受类Shape的实例作为其子项。请考虑以下代码:

public class SvgGroup : Canvas
{

    // ...

    public Brush Fill
    {
        // Retuns the fill brush value of all the shape children, if they are all the same. Otherwise, the default value of Brush is returned
        get
        {
            Brush rtn = default(Brush);
            for (int i = 0; i < ShapeChildren.Count; i++)
            {
                Shape shape = ShapeChildren[i];
                if (i == 0) // First loop
                {
                    rtn = shape.Fill;
                }
                else if (rtn != shape.Fill) // Children shapes have different Fill value
                {
                    return default(Brush);
                }
            }

            return rtn;
        }

        // Sets the fill brush value of all the shape children
        set
        {
            foreach (Shape shape in ShapeChildren)
            {
                shape.Fill = value;
            }
        }
    }

    // ...
}

问题是在XAML中设置Fill属性时,没有任何反应。但是,设置Fill in code-behind工作。

我在考虑依赖属性,但在这种情况下的实现可能非常棘手。

2 个答案:

答案 0 :(得分:1)

我认为您应该定义两个依赖项属性,并且应该更新其中一个:

public class SvgGroup : Canvas
{

    public Brush Fill
    {
        get { return (Brush)GetValue(FillProperty); }
        set { SetValue(FillProperty, value); }
    }
    public static readonly DependencyProperty FillProperty
   = DependencyProperty.Register(
         "Fill",
         typeof(Brush),
         typeof(SvgGroup), 
         new FrameworkPropertyMetadata(Brushes.Red, OnFillPropertyChanged)
     );

    private static void OnFillPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        SvgGroup svg = (SvgGroup)d;
        if (e.NewValue != null && !e.NewValue.Equals(e.OldValue))
        {
            foreach (Shape shape in d.ShapeChildren)
            {
                shape.Fill = (Brush)e.NewValue;
            }
            d.OnShapeBrushChanged(); // Note that you should call this method in some other places too.
        }
    }

    public Brush FillDifferentBrush
    {
        get { return (Brush)GetValue(IsFillDifferentProperty); }
    }

    public static readonly DependencyProperty FillDifferentProperty
        = DependencyProperty.Register(
              "FillDifferentBrush",
              typeof(Brush),
              typeof(SvgGroup),
              new PropertyMetadata(null)
          );




    void OnShapeBrushChanged()
    {
        Brush rtn = default(Brush);
        for (int i = 0; i < ShapeChildren.Count; i++)
        {
            Shape shape = ShapeChildren[i];
            if (i == 0) // First loop
            {
                rtn = shape.Fill;
            }
            else if (rtn != shape.Fill) // Children shapes have different Fill value
            {
                SetValue(FillDifferentProperty, default(Brush));
            }
            else
                SetValue(FillDifferentProperty, rtn);
        }
    }

}

您应该正确调用OnShapeBrushChanged()(例如,当您添加新的Shapes或单独更改其Brush时,或者当您调用Fill属性时)以保持更新(类似于ItemsControl的HasItems属性)。 / p>

答案 1 :(得分:0)

您可以声明Attached Property行为Property Value Inheritance

在任何父元素上设置属性时,其值将由所有子元素继承。有一个PropertyChanged回调函数,用于检查元素是否为Shape,并最终将继承的Brush应用于Shape的Fill属性。

public static class ChildFillEx
{
    public static readonly DependencyProperty ChildFillProperty =
        DependencyProperty.RegisterAttached(
            "ChildFill", typeof(Brush), typeof(ChildFillEx),
            new FrameworkPropertyMetadata(
                null,
                FrameworkPropertyMetadataOptions.Inherits,
                ChildFillPropertyChanged));

    public static Brush GetChildFill(DependencyObject obj)
    {
        return (Brush)obj.GetValue(ChildFillProperty);
    }

    public static void SetChildFill(DependencyObject obj, Brush value)
    {
        obj.SetValue(ChildFillProperty, value);
    }

    private static void ChildFillPropertyChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var shape = obj as Shape;
        if (shape != null)
        {
            shape.Fill = (Brush)e.NewValue;
        }
    }
}

您可以这样使用它:

<Canvas local:ChildFillEx.ChildFill="Red">
    <Rectangle Width="100" Height="100" />
</Canvas>