There is a UserControl, naturally being a DependencyObject'om for which you need to create your DependencyProperty like this:

<my:TLBUserControl HorizontalAlignment="Left" Margin="230,25,0,0" x:Name="tLBUserControl1" VerticalAlignment="Top" /> 

where HorizontalAlignment="Left" is the DependencyProperty. How to do it most correctly. Leave good articles on this topic, and even better code samples. Thank you in advance for your help.

    1 answer 1

    1. The usual dependency property (also affecting the own redraw in the example). In the FrameworkPropertyMetadata constructor, you can define additional settings, such as the method for correcting a value or processing a changed value. MSDN or WPF 3.0-4.0 for Professionals

       #region Единичный размер (NormalSize) [Category("Group properties")] public Size NormalSize { get { return (Size)GetValue(NormalSizeProperty); } set { SetValue(NormalSizeProperty, value); } } public static readonly DependencyProperty NormalSizeProperty = DependencyProperty.Register("NormalSize", typeof(Size), typeof(GroupPanel), new FrameworkPropertyMetadata(new Size(200, 190)) { AffectsArrange = true, AffectsMeasure = true }); #endregion 
    2. There is a very useful type of dependent properties - the so-called. Attatched Property. Meaning such as, for example, Grid.Row or Canvas.Top. it can be "attached" to any DependencyObject and then already used directly in the control. At initialization, a little more code is required, but it is ideologically justified without it in any way. Here is an example of Attatched Property.

       static GroupPanel() // - статический конструктор класса { var metadata = new FrameworkPropertyMetadata(); metadata.AffectsParentArrange = true; - различные атрибуты metadata.AffectsParentMeasure = true; BreakBeforeProperty = DependencyProperty.RegisterAttached( "ColumnBreakBefore", typeof(bool), typeof(GroupPanel), metadata); } #region Свойство разрыва (BreakBefore) public static DependencyProperty BreakBeforeProperty; public static void SetColumnBreakBefore(UIElement element, Boolean value) { element.SetValue(BreakBeforeProperty, value); } public static Boolean GetColumnBreakBefore(UIElement element) { return (bool)element.GetValue(BreakBeforeProperty); } #endregion 

    Note on use. For Attatched Property to work properly and be recognized by the system, you must comply with the convention for naming static methods.

    public static тип свойства Get(Set)имя свойства(тип объекта) . I hope this is clear. In my example this

    public static Boolean GetColumnBreakBefore(UIElement element) { }