【发布时间】:2013-06-15 17:40:31
【问题描述】:
我需要能够从子 ItemsControl 数据模板内部绑定到父 ItemsControl 的属性:
<ItemsControl ItemsSource="{Binding Path=MyParentCollection, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Path=MySubCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=MyParentCollection.Value, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
假设 MyParentCollection(外部集合)属于以下类型:
public class MyObject
{
public String Value { get; set; }
public List<MyChildObject> MySubCollection { get; set;
}
让我们假设上述类中的 MyChildObject 属于以下类型:
public class MyChildObject
{
public String Name { get; set; }
}
如何从内部数据模板内部绑定到 MyParentCollection.Value?我不能真正按类型使用 FindAncestor,因为它们所有级别都使用相同的类型。我想也许我可以在外部集合上放一个名称并在内部绑定中使用 ElementName 标记,但这仍然无法解析该属性。
有什么想法吗?
【问题讨论】:
-
不能使用 FindAncestor 模式,同时指定 Type 和 Ancestor 级别吗?
-
hmmm,没想到祖先级别……我去看看
-
我似乎无法让它工作,哦,好吧