如何在UserControl中使用计算和自定义DependecyProperty?

时间:2016-08-29 20:11:44

标签: wpf user-controls dependency-properties

我想为项目制作一个 EllipticArc ,然后我创建了一个 UserControl 来制作它。在我的代码中,我需要根据EndPointAngleRadius计算 Arc IsLargeArc。我在下面写了代码,但它没有用,所以设计师没有任何东西出现。我无法理解为什么会这样。我该如何解决?

EllipticArc.cs

public partial class EllipticArc: UserControl {
public EllipticArc() {
    InitializeComponent();
}

public double Radius {
    get {
        return (int) GetValue(RadiusProperty);
    }
    set {
        SetValue(RadiusProperty, value);
    }
}

// Using a DependencyProperty as the backing store for Yaricap.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register("Radius", typeof(double), typeof(EllipticArc), new PropertyMetadata(20 d));

public Point BeginPoint {
    get {
        return (Point) GetValue(BeginPointProperty);
    }
    set {
        SetValue(BeginPointProperty, value);
    }
}

// Using a DependencyProperty as the backing store for BeginPoint.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty BeginPointProperty = DependencyProperty.Register("BeginPoint", typeof(Point), typeof(EllipticArc), new PropertyMetadata(new Point(0 d, 0 d)));

public double Angle {
    get {
        return (int) GetValue(AngleProperty);
    }
    set {
        SetValue(AngleProperty, value);
    }
}

// Using a DependencyProperty as the backing store for Angle.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(EllipticArc), new PropertyMetadata(90 d));

public bool IsLarge {
    get {
        SetValue(IsLargeProperty, Angle >= 180 ? true: false);
        return (bool) GetValue(IsLargeProperty);
    }
    set {
        SetValue(IsLargeProperty, value);
    }
}

// Using a DependencyProperty as the backing store for IsLarge.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsLargeProperty = DependencyProperty.Register("IsLarge", typeof(bool), typeof(EllipticArc), new PropertyMetadata(false));

Point CalculateEndPoint(Point beginPoint, double radius, double angle) {

    beginPoint.X = beginPoint.X + radius;
    beginPoint.Y = beginPoint.Y + radius;
    double xplus = Math.Sin(angle) * radius + beginPoint.X;
    double yplus = Math.Cos(angle) * radius + beginPoint.Y;
    return new Point(xplus, yplus);
}
public Point EndPoint {
    get {
        SetValue(EndPointProperty, CalculateEndPoint(BeginPoint, Radius, Angle));
        return (Point) GetValue(EndPointProperty);
    }
    set {
        SetValue(EndPointProperty, value);
    }
}

// Using a DependencyProperty as the backing store for EndPoint.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register("EndPoint", typeof(Point), typeof(EllipticArc), new PropertyMetadata(0));

public Size BarSize {
    get {
        SetValue(BarSizeProperty, new Size(Radius * 2, Radius * 2));
        return (Size) GetValue(BarSizeProperty);
    }
    set {
        SetValue(BarSizeProperty, value);
    }
}

// Using a DependencyProperty as the backing store for BarSize.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty BarSizeProperty = DependencyProperty.Register("BarSize", typeof(Size), typeof(EllipticArc), new PropertyMetadata(new Size(40, 40)));

}

EllipticArc.xaml

<UserControl x:Class="WpfApplication2.EllipticArc"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <Grid>
        <Path>
            <Path.Data>
                <PathGeometry>
                    <PathFigure StartPoint="{Binding BeginPoint}">
                        <ArcSegment Size="{Binding BarSize}" Point="{Binding EndPoint}"
                                    SweepDirection="Clockwise" IsLargeArc="{Binding IsLarge}"
                                    RotationAngle="0"/>
                    </PathFigure>
                </PathGeometry>
            </Path.Data>
        </Path>
    </Grid>
</UserControl>

0 个答案:

没有答案