wpf custom control property of type class

essamce 621 Reputation points
2020-06-25T12:42:24.607+00:00

hi i'm trying to create a custom control with a public property Boundary of type MovingBoundary(as below), but when i'm trying to use my custom control in a xaml i cant't initialize Boundary property.

  <custom:MyCustomControl
                    x:Name="m1"
                    Canvas.Left="0" 
                    Canvas.Top="0"

                    Boundry.Top="100"   />

line6 gives erro.

any help will be apprciated.

 public class MovingBoundry : ViewModelBase
    {
        private double _left = double.MinValue;
        public double Left
        {
            get
            {
                return _left;
            }
            set
            {
                if (value == _left) return;
                _left = value;
                RaisePropertyChanged(nameof(Left));
            }
        }

        private double _right = double.MaxValue;
        public double Right
        {
            get
            {
                return _right;
            }
            set
            {
                if (value == _right) return;
                _right = value;
                RaisePropertyChanged(nameof(Right));
            }
        }

        private double _top = double.MinValue;
        public double Top
        {
            get
            {
                return _top;
            }
            set
            {
                if (value == _top) return;
                _top = value;
                RaisePropertyChanged(nameof(Top));
            }
        }

        private double _bottom = double.MaxValue;
        public double Bottom
        {
            get
            {
                return _bottom;
            }
            set
            {
                if (value == _bottom) return;
                _bottom = value;
                RaisePropertyChanged(nameof(Bottom));
            }
        }


    }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,316 Reputation points
    2020-06-25T15:43:46.99+00:00

    Hi, change your declaration:

    public sealed partial class MyCustomControl : UserControl
    
    …
    
             public static readonly DependencyProperty BoundryProperty =
                 DependencyProperty.Register("Boundry", typeof(MyCustomControl), typeof(MovingBoundry), new PropertyMetadata(new MovingBoundry()));
    
    1 person found this answer helpful.