开发者

Silverlight DependencyProperty problems

开发者 https://www.devze.com 2022-12-15 05:02 出处:网络
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 pro

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 act开发者_高级运维s 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.


I suspect you need to review what actually happens when you "hide" a member of a parent type. Any code that uses the parent type will not see your new definition, it will continue to use the existing member defined by the parent. Only code that is written against your new derived type will start using the new definition.

In this case when a reference to your custom control is held as a Control type any access to IsTabStop will return the implementation in Control. Only when code knows its working against the type CustomTextBox will it use your custom definition.

0

精彩评论

暂无评论...
验证码 换一张
取 消