这让我快疯了。也许我的数据设计错误,但我试图从父ItemsControl绑定到活动项。
每个区域都有一个颜色,以便在屏幕上很容易识别。我没有给每个座位赋予颜色属性,而是在区域模型中植入了颜色。所有儿童座椅都需要用这种颜色来油漆自己。
无论我尝试什么,我都无法到达父ItemsControl,因此我可以获取该特定区域的活动颜色属性。
模型(简化)
public class BuildingLayout
{
public ObservableCollection<Area> Areas { get; set; }
}
public class Area
{
public Color Color { get; set; } // The color I want to bind to
...
public ObservableCollection<Table> Tables { get; set; }
}
public class Table
{
public int Seat { get; set; } // The seat needs to get the area color
}XAML
<ItemsControl ItemsSource="{Binding Path=Areas}">
...
<ItemsControl.ItemTemplate>
<!-- Nested Listbox -->
<ItemsControl ItemsSource="{Binding Path=Tables}">
...
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- Now I want to get to the color property of
the parent (Area) -->
<Rectangle Fill="{Binding ..., Path=Color}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ItemsControl.ItemTemplate>
</ItemsControl>发布于 2013-07-29 21:24:34
<Rectangle Fill="{Binding DataContext.Color, RelativeSource={RelativeSource AncestorType=ItemsControl}"/>因为嵌套的ItemsControl位于父ItemTemplate中,因此它将具有Area实例作为DataContext。
https://stackoverflow.com/questions/17934361
复制相似问题