2011-11-22 71 views
2

标题可能听起来令人费解,但忍受着我。内部ItemsControl中的对象的属性如何绑定到外部ItemsControl中的对象的属性?

我有包含住户房间:

public class Room 
{ 
    public string Name { get; set; } 
    public List<Person> Occupants { get; set; } 
    public bool AreOccupantsEditable { get; set; } 
} 

public class Person 
{ 
    public string Name { get; set; } 
} 

这里的客房数组:

<ResourceDictionary> 
    <x:Array x:Key="Rooms" Type="local:Room"> 
     <local:Room Name="Happy Room" AreOccupantsEditable="True"> 
      <local:Room.Occupants> 
       <local:Person Name="Mindy" /> 
      </local:Room.Occupants> 
     </local:Room> 
     <local:Room Name="Sad Room" AreOccupantsEditable="True"> 
      <local:Room.Occupants> 
       <local:Person Name="Bob" /> 
       <local:Person Name="Jane" /> 
      </local:Room.Occupants> 
     </local:Room> 
     <local:Room Name="Kill Room" AreOccupantsEditable="False"> 
      <local:Room.Occupants> 
       <local:Person Name="Mork" /> 
       <local:Person Name="Dave" /> 
       <local:Person Name="Ryan" /> 
      </local:Room.Occupants> 
     </local:Room> 
    </x:Array> 
</ResourceDictionary> 

这里是一个ItemsControl在一个ItemsControl,显示房间和他们的居住者:

<ItemsControl ItemsSource="{Binding Source={StaticResource Rooms}}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <!-- room name --> 
       <TextBlock Text="{Binding Path=Name}" /> 
       <ItemsControl ItemsSource="{Binding Path=Occupants}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <!-- occupant name --> 
          <TextBox Text="{Binding Path=Name}" Margin="20,0,0,0" IsEnabled="{Binding ???}" /> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

没有人提及房间,我如何绑定T的IsEnabled属性extBox转到Are a Room的人的AreOccupantsEditable属性?

如果有帮助,下面是一个示例项目:http://dl.dropbox.com/u/4220513/ItemsControl-Binding.zip

回答

3

您可以使用RelativeSource访问外DataContext

IsEnabled="{Binding Path=DataContext.AreOccupantsEditable, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" 
+0

我正在玩RelativeSource和AncestorType,但我无法得到它的工作。明天早上我会说这个。 – epalm

+0

对,这正是我所需要的。谢谢 – epalm

0

你也可以用居住者的itemControl的IsEnabled属性禁用整个itemControl

<ItemsControl ItemsSource="{Binding Path=Occupants}" IsEnabled="{Binding Path=AreOccupantsEditable}"> 
相关问题