The dependency property does not work.

UserControl.cs:

// private double Radius = 100; private void DrawScale() { double bigTick = 9; for (double i = 0; i <= 90; i = i + bigTick) { Point p = new Point(0.5, 0.5); Rectangle r = new Rectangle { Height = 5, Width = 15, Fill = new SolidColorBrush(Colors.Aqua), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, RenderTransformOrigin = p }; TransformGroup trGp = new TransformGroup(); RotateTransform rTr = new RotateTransform(); double iRadian = (i * Math.PI) / 180; rTr.Angle = i; trGp.Children.Add(rTr); TranslateTransform tTr = new TranslateTransform(); tTr.X = (int) ((Radius) * Math.Cos(iRadian)); tTr.Y = (int) ((Radius) * Math.Sin(iRadian)); trGp.Children.Add(tTr); r.RenderTransform = trGp; gr.Children.Add(r); } } public double Radius { get { return (double)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("Radius", typeof(double), typeof(RoundScale), null); 

userControl.xaml

  <Grid x:Name="gr"/> 

MainWindow.xaml

  <Grid> <local:RoundScale Radius="90" /> </Grid> 

If the Radius variable in UserControl.cs is uncommented and the dependency property is removed from the code, then everything works as it should.

    1 answer 1

    You have incorrectly defined the dependency property. The CLR property should be named as your dependency property, minus the Property suffix. In your case, the dependency property is called MyPropertyProperty , and the CLR property is called Radius .

    Rename the dependency property to RadiusProperty .


    Documentation: https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/custom-dependency-properties#checklist-for-defining-a-dependency-property :

    If you create an identifier field, you must also register it.


    Another problem is the query dependency property from the constructor. The point is that the dependency property specified directly in the XAML is set to InitializeComponent , and bindings can work even later. Therefore, it makes sense to either read the value on the Loaded event or subscribe to changes.


    If you need to subscribe to changes, the easiest way is to specify the callback in the definition of the dependency property:

     public double Radius { get { return (double)GetValue(RadiusProperty); } set { SetValue(RadiusProperty, value); } } public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register( "Radius", typeof(double), typeof(RoundScale), new PropertyMetadata(0.0, OnRadiusChangedStatic)); static void OnRadiusChangedStatic(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((RoundScale)d).OnRadiusChanged(); void OnRadiusChanged() { // тут у вас поменялся радиус, реагируйте } 
    • Thank you very much for the help, but it still does not work - XmaksasX
    • @XmaksasX: So there’s some kind of mistake. And your class in which dependency property lies is called RoundScale or not? - VladD
    • Yes class name RoundScale - XmaksasX
    • @XmaksasX: Hmm. And why in your example is not <local:RoundScale Radius="90"/> , but <local:UserControl2 Radius="90" /> ? - VladD
    • Sorry corrected - XmaksasX