2013-03-26 52 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元素創建一個新的列表實例後