2013-03-26 55 views
0

当我尝试将附加属性集合添加到面板时,面板内的附加属性集合

<Grid x:Name="MainContent" Grid.Row="1"> 
    <StackPanel x:Name="MainContentSp"> 

     <do:AttachCollection.Col> 
      <do:AttachItem x="y"/> 
      <do:AttachItem x="z"/> 
     </do:AttachCollection.Col> 

     <TextBlock x:Name="tb1" Text="xx"/> 
     <TextBlock x:Name="tb2" Text="yy"/> 

    </StackPanel> 
</Grid> 

它将它分配给网格,而不是...我如何将它附加到面板?

回答

0

Mkay事实证明,附加的属性是与每个单一的UI元素共享一个列表实例,所以我不得不用一种相当黑客的方式给每个元素它自己的列表实例。

public static List<X> GetX(UIElement element) 
{ 
    var list = ((List<X>)(element.GetValue(XProperty))); 
    if (list == null) 
    { 
     list = new List<X>(); 
     SetX(element, list); 
    } 
    return list; 
} 

之前

public static readonly 
    DependencyProperty 
    XProperty = 
     DependencyProperty 
      .RegisterAttached 
      ("X", 
       typeof(List<X>), 
       typeof(X), 
       new PropertyMetadata(new List<X>())); 

public static readonly 
    DependencyProperty 
    XProperty = 
     DependencyProperty 
      .RegisterAttached 
-->   ("XInternal", 
       typeof(List<X>), 
       typeof(X)); 
-->(removed) 

随着现在吸气每个UI元素创建一个新的列表实例后