FrameworkPropertyMetadata.Inherits indicates whether the DependencyProperty registered with this metadata is inheritable or not. For example, Binding.DataContextProperty is inheritable, yet DockPanel.DockProperty is not.
The demo code below firstly creates a DockPanel, a StackPanel, and a Button. To differentiate between getting a value from inheritance and getting the default value, we then set DockProperty value for stackPanel1 to be Dock.Top. We also set DataContextProperty for stackPanel1 to be dockPanel1. (Default values for DockProperty and DataContextProperty are Dock.Left and null respectively.)
Before and after we add button1 into tree, we print out its values for DockProperty and DataContextProperty. It clearly shows the effect of property inheritance.
You can put all the code inside Click event handler for a button.
// Sample event handler:
private void ButtonClick(object sender, RoutedEventArgs e)
{
DockPanel dockpanel1 = new DockPanel();
StackPanel stackPanel1 = new StackPanel();
Button button1 = new Button();
DockPanel.SetDock(stackPanel1, Dock.Top);
Binding.SetDataContext(stackPanel1, dockpanel1);
System.Diagnostics.Debug.WriteLine("button1.DockProperty is " + DockPanel.GetDock(button1).ToString());
object value1 = Binding.GetDataContext(button1);
System.Diagnostics.Debug.WriteLine("button1.DataContextProperty is " + (value1==null?"null":value1.ToString()));
dockpanel1.Children.Add(stackPanel1);
stackPanel1.Children.Add(button1);
System.Diagnostics.Debug.WriteLine("button1.DockProperty is " + DockPanel.GetDock(button1).ToString());
value1 = Binding.GetDataContext(button1);
System.Diagnostics.Debug.WriteLine("button1.DataContextProperty is " + (value1 == null ? "null" : value1.ToString()));
}
The output is as follows:
button1.DockProperty is Left
button1.DataContextProperty is null
button1.DockProperty is Left
button1.DataContextProperty is System.Windows.Controls.DockPanel
There are various other ways to affect a DependencyProeprty’s value. More blogs to come.
(This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)