![](/img/trans.png)
[英]Cannot bind an Observable Collection to an Attached Property on an UserControl
[英]Attached Property collection inside a Panel
當我嘗試將附加的屬性集合添加到Panel時,就像這樣;
<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>
而是將其分配給網格...如何將其附加到面板?
Mkay原來,附加屬性是與每個ui元素共享一個列表實例,因此我不得不采用一種頗為怪異的方式將每個元素賦予自己的列表實例。
之前
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)
現在,使用getter為每個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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.