Working as WPF dependency properties (DependencyProperty)?

Hello!
Let's say I implement a dependency property in a class derived from Button:
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Petzold.SetSpaceProperty
{
 public class SpaceButton: Button
{
 string txt;

 public string Text
{
set
{
 txt = value;
 Content = SpaceOutText(txt);
}
get
{
 return txt;
}
}

 public static readonly DependencyProperty SpaceProperty;

 public int Space
{
set
{
 SetValue(SpaceProperty, value);
}
get
{
 return (int)GetValue(SpaceProperty);
}
}

 static SpaceButton()
{
 FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
 metadata.DefaultValue = 1;
 metadata.AffectsMeasure = true;
 metadata.Inherits = true;
 metadata.PropertyChangedCallback += OnSpacePropertyChanged;

 SpaceProperty = DependencyProperty.Register("Space", typeof(int), typeof(SpaceButton), metadata, ValidateSpaceValue);
}

 private static bool ValidateSpaceValue(object obj)
{
 int i = (int)obj;
 return i >= 0;
}

 private static void OnSpacePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
 SpaceButton btn = obj as SpaceButton;
 btn.Content = btn.SpaceOutText(btn.Text);
}

 private object SpaceOutText(string str)
{
 if (str == null)
 return null;

 StringBuilder build = new StringBuilder();
 foreach (char ch in str)
 build.Append(ch + new string(' ', Space));

 return build.ToString();
}
}
}

Then I add the new owner in the class derived from Window:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Petzold.SetSpaceProperty
{
 public class SpaceWindow: Window
{
 public static readonly DependencyProperty SpaceProperty;
 public int Space
{
set
{
 //SetValue(SpaceProperty, value);
 SetValue(SpaceButton.SpaceProperty, value);
}
get
{
 //return (int)GetValue(SpaceProperty);
 return (int)GetValue(SpaceButton.SpaceProperty);
}
}

 static SpaceWindow()
{
 FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata() { Inherits = true};

 SpaceProperty = SpaceButton.SpaceProperty.AddOwner(typeof(SpaceWindow));
 SpaceProperty.OverrideMetadata(typeof(SpaceWindow), metadata); 
}
}
}

Tell me, please, why in the class SpaceWindow allowed using the following code:
SetValue(SpaceButton.SpaceProperty, value);
although this class has already overridden this property (SpaceProperty)?
What is the difference between SetValue(SpaceButton.SpaceProperty, value);and SetValue(SpaceProperty, value);?
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Petzold.SetSpaceProperty
{
 public class SetSpaceProperty : SpaceWindow
{
[STAThread]
 public static void Main()
{
 Application app = new Application();
 app.Run(new SetSpaceProperty());
}
 public SetSpaceProperty()
{
 Title = "Set space property";
 SizeToContent = SizeToContent.WidthAndHeight;
 ResizeMode = ResizeMode.CanMinimize;
 int[] iSpaces = { 0, 1, 2 };

 Grid grid = new Grid();
 Content = grid;

 for(int i = 0; i < 2; ++i)
{
 RowDefinition row = new RowDefinition() { Height = GridLength.Auto };
grid.RowDefinitions.Add(row);
}
 for(int i = 0; i < iSpaces.Length; ++i)
{
 ColumnDefinition col = new ColumnDefinition() { Width = GridLength.Auto};
grid.ColumnDefinitions.Add(col);
}
 for(int i = 0; i < iSpaces.Length; ++i)
{
 SpaceButton btn = new SpaceButton() { Text = "Set window Space to "+iSpaces[i], Tag = iSpaces[i], HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center};
 btn.Click += WindowPropertyOnClick;
grid.Children.Add(btn);
 Grid.SetRow(btn, 0);
 Grid.SetColumn(btn, i);

 btn = new SpaceButton() { Text = "Set button Space to" + iSpaces[i], Tag = iSpaces[i], HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
 btn.Click += ButtonPropertyOnClick;
grid.Children.Add(btn);
 Grid.SetRow(btn, 1);
 Grid.SetColumn(btn, i);
}
}

 private void ButtonPropertyOnClick(object sender, RoutedEventArgs args)
{
 SpaceButton btn = args.Source as SpaceButton;
 btn.Space = (int)btn.Tag;
}

 private void WindowPropertyOnClick(object sender, RoutedEventArgs args)
{
 SpaceButton btn = args.Source as SpaceButton;
 Space = (int)btn.Tag;
}
}
}
March 23rd 20 at 19:01
0 answer

Find more questions by tags SpecificationsC#WPF.NET