PropertyChangedCallback - 第一次通话?

时间:2013-11-18 19:18:54

标签: wpf dependency-properties

每次更改属性时都会调用 OnIconScaleChange IconScale 但从未打电话给创业公司。 (默认值= 50.0) 告诉我如何在创建UserControl时第一次调用代码 OnIconScaleChange

private static void OnIconScaleChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    MyControl c = (MyControl)d;

    if (c != null)
    {
        double v = (double)e.NewValue;
        ScaleTransform scaleTransform = new ScaleTransform(v, v);
        c.RenderTransform = scaleTransform;
    }
}

public double IconScale
{
    get { return (double)GetValue(IconScaleProperty); }
    set { SetValue(IconScaleProperty, value); }
}

public static readonly DependencyProperty IconScaleProperty =
 DependencyProperty.Register("IconScale", typeof(double), typeof(MyControl), new FrameworkPropertyMetadata(50.0, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnIconScaleChange)));

1 个答案:

答案 0 :(得分:1)

在MyControl的构造函数中初始化RenderTransform

public MyControl()
{
    InitializeComponent();
    RenderTransform = new ScaleTransform(IconScale, IconScale);
}