2011-11-22 78 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}"> 
相關問題