Here’s the situation: I have a custom TextBox control that contains multiple other TextBox controls. I need to turn IsTabStop off in the parent control, but I still want to expose a new IsTabStop property, which the sub-textboxes are template-bound to. I wrote the following code:
using System.Windows.Controls;
public class CustomTextBox : Control {
public CustomTextBox() {
base.IsTabStop = false;
}
[Description( "Gets or sets whether a control is included in tab navigation." )]
[Category( "Common Properties" )]
public new bool IsTabStop
{
get { return (bool)GetValue( IsTabStopProperty ); }
set { SetValue( IsTabStopProperty, value ); }
}
public new static readonly DependencyProperty IsTabStopProperty = DependencyProperty.Register(
"IsTabStop",
typeof( bool ),
typeof( CustomTextBox ),
new PropertyMetadata( true ) );
}
But that results in strange behavior. When IsTabStop is not specified for instances of the custom controls, it acts like IsTabStop is false, even though the default is true. If IsTabStop is explicitly marked true, though, the base class’s IsTabStop is set to true. Also, if I rename “IsTabStop” and all related text (including the bindings) to “IsTabStopx”, thus not hiding the base member, it works as desired. Shouldn’t a hidden member act the same as a brand new definition? Could something somewhere be reading the base class’s IsTabStop?
What’s going on?
The DependencyProperty system operates independently of the C# property getters and setters which are provided as a convienience to the programmer.
WPF/Silverlight will read the Control.IsTabStopProperty directly and will not use the CustomTextBox.IsTabStop property or the CustomTextBox.IsTabStopProperty DependencyProperty.