首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从嵌套的ItemsControl绑定到active ItemsControl项

如何从嵌套的ItemsControl绑定到active ItemsControl项
EN

Stack Overflow用户
提问于 2013-07-29 21:17:43
回答 1查看 1.5K关注 0票数 2

这让我快疯了。也许我的数据设计错误,但我试图从父ItemsControl绑定到活动项。

每个区域都有一个颜色,以便在屏幕上很容易识别。我没有给每个座位赋予颜色属性,而是在区域模型中植入了颜色。所有儿童座椅都需要用这种颜色来油漆自己。

无论我尝试什么,我都无法到达父ItemsControl,因此我可以获取该特定区域的活动颜色属性。

模型(简化)

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-29 21:24:34

代码语言:javascript
复制
<Rectangle Fill="{Binding DataContext.Color, RelativeSource={RelativeSource AncestorType=ItemsControl}"/>

因为嵌套的ItemsControl位于父ItemTemplate中,因此它将具有Area实例作为DataContext。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17934361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档