Initialize RenderTransform
in the constructor of MyControl:
public MyControl()
{
InitializeComponent();
RenderTransform = new ScaleTransform(IconScale, IconScale);
}
سؤال
OnIconScaleChange called every time when I change the property IconScale but never called on startup. (with default value = 50.0 ) Tell me how to modify the code to OnIconScaleChange invoked the first time on create a UserControl?
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)));
المحلول
Initialize RenderTransform
in the constructor of MyControl:
public MyControl()
{
InitializeComponent();
RenderTransform = new ScaleTransform(IconScale, IconScale);
}