WPF简单自定义形状

时间:2014-05-16 17:39:58

标签: c# wpf wpf-controls

我创建了一个简单的自定义形状类和两个属性“ActiveColor”(类型:颜色)和“活动”(类型:bool)。当我将Active设置为true时,形状应该改变ActiveColor ....中定义的颜色,颜色仍然是透明的....

关注我的自定义课程:

public class Ellipse2 : Shape
{


    public bool Active
    {
        get { return (bool)GetValue(ActiveProperty); }
        set { SetValue(ActiveProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Active.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ActiveProperty =
        DependencyProperty.Register("Active", typeof(bool), typeof(Ellipse2), new UIPropertyMetadata(ActiveChanged));



    public Color ActiveColor
    {
        get { return (Color)GetValue(ActiveColorProperty); }
        set { SetValue(ActiveColorProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ActiveColor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ActiveColorProperty =
        DependencyProperty.Register("ActiveColor", typeof(Color), typeof(Ellipse2), new PropertyMetadata(Colors.Transparent));



    EllipseGeometry ellipse;

    private static void ActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var shape = d as Ellipse2;
        var color = shape.Active ? shape.ActiveColor : Colors.Transparent;
        shape.Fill = new SolidColorBrush(color);
    }


    public Ellipse2()
    {
        ellipse = new EllipseGeometry();
        this.Stroke = Brushes.Gray;
        this.StrokeThickness = 1;
    }
    protected override Geometry DefiningGeometry
    {
        get
        {
           TranslateTransform t = new TranslateTransform(ActualWidth / 2, ActualHeight / 2);
            ellipse.Transform = t;
            ellipse.RadiusX = this.ActualWidth / 2;
            ellipse.RadiusY = this.ActualHeight / 2;
            return ellipse;
        }
    }
}  

XAML用法:

                                <StackPanel Orientation="Horizontal">
                                    <controls:Ellipse2 ActiveColor="Red" Height="{StaticResource height}" Width="{StaticResource width}" Active="False" />
                                    <controls:Ellipse2 ActiveColor="Blue" Height="{StaticResource height}" Width="{StaticResource width}"  Active="False" />
                                    <controls:Ellipse2 ActiveColor="Yellow" Height="{StaticResource height}" Width="{StaticResource width}" Active="True"  />
                                    <controls:Ellipse2 ActiveColor="Orange" Height="{StaticResource height}" Width="{StaticResource width}" Active="True"  />
                                    <controls:Ellipse2 ActiveColor="Black" Height="{StaticResource height}" Width="{StaticResource width}" Active="True"  />
                                    <controls:Ellipse2 ActiveColor="Pink" Height="{StaticResource height}" Width="{StaticResource width}" Active="False"  />
                                    <controls:Ellipse2 ActiveColor="Green" Height="{StaticResource height}" Width="{StaticResource width}" Active="False"  />
                                </StackPanel>

当Active属性发生变化时,我希望在Active设置为TRUE时用ActiveColor中定义的颜色填充椭圆。我的代码出了什么问题?

TKS。

0 个答案:

没有答案