日期:2014-05-18 浏览次数:21049 次
public class MyButton : Button {
    public MyButton() {
        HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
        VerticalAlignment = System.Windows.VerticalAlignment.Center;
    }
    public double WidthPercentage {
        get { return (double)GetValue(WidthPercentageProperty); }
        set { SetValue(WidthPercentageProperty, value); }
    }
    public double HeightPercentage {
        get { return (double)GetValue(HeightPercentageProperty); }
        set { SetValue(HeightPercentageProperty, value); }
    }
    public static readonly DependencyProperty HeightPercentageProperty =
        DependencyProperty.Register("HeightPercentage", typeof(double), typeof(MyButton), new UIPropertyMetadata(0.0));
    public static readonly DependencyProperty WidthPercentageProperty =
        DependencyProperty.Register("WidthPercentage", typeof(double), typeof(MyButton), new UIPropertyMetadata(0.0));
    protected override Size MeasureOverride(Size constraint) {
        var result = new Size(constraint.Width * WidthPercentage, constraint.Height * HeightPercentage);
        return result;
    }
    protected override Size ArrangeOverride(Size arrangeBounds) {
        return base.ArrangeOverride(arrangeBounds);
    }
}