2010-07-16 46 views
0

试图为WPF DependencyObject创建我自己的自定义AttachedProperty未能真正做到我希望它做的事情,而且我有点担心我(再次)完全不理解WPF概念。AttachedProperty不传播给孩子?

我做了一个非常简单的测试课程,以显示我的问题在哪里。从the MSDN Documentation,我复制

public class TestBox : TextBox 
{ 
    public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
      "IsBubbleSource", 
      typeof(Boolean), 
      typeof(TestBox) 
      ); 
    public static void SetIsBubbleSource(UIElement element, Boolean value) 
    { 
     element.SetValue(IsBubbleSourceProperty, value); 
    } 
    public static Boolean GetIsBubbleSource(UIElement element) 
    { 
     return (Boolean)element.GetValue(IsBubbleSourceProperty); 
    } 
    public Boolean IsBubbleSource 
    { 
     get 
     { 
      return (Boolean)GetValue(IsBubbleSourceProperty); 
     } 
     set 
     { 
      SetValue(IsBubbleSourceProperty, value); 
     } 
    } 
} 

现在,把我的新时髦的文本框为一个网格这样

<Grid vbs:TestBox.IsBubbleSource="true"> 
    <vbs:TestBox x:Name="Test" Text="Test" >      
    </vbs:TestBox> 
</Grid> 

我希望每一个不设置IsBubbleSource属性本身来自于“继承”它的孩子其母公司网格。它不这样做;一个MessageBox.Show(Test.IsBubbleSource.ToString())显示“错误”。附加属性设置为true。我使用OnPropertyChanged事件处理程序检查了这一点。我错过了什么?

谢谢!

回答

2

默认情况下,附加属性不会被继承。您必须在定义属性时指定它:

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
    "IsBubbleSource", 
    typeof(Boolean), 
    typeof(TestBox), 
    new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits) 
    ); 
+0

是的,就是这样。谢谢!我想知道如果它们没有被继承,它们有什么用处...... – Jens 2010-07-16 11:51:04