2011-08-01 43 views
0

我有一個DataTemplate模板我的ItemsControl的項目是TimeSheet的詳細信息。 我有幾個TextBox代表我的TimeSheet的Details的某些值,但他們的IsEnabled屬性取決於TimeSheet本身,而不是細節。超越DataContext的綁定

<ItemsControl 
    ItemsSource="{Binding Path=TimeSheet.TimeSheetDetails}" 
    ItemTemplate="{StaticResource TimeSheetDetail}" 
/> 

<DataTemplate x:Key="TimeSheetDetail"> 
    <TextBox 
     Text="{Binding Houre}" 
     IsEnabled="Binding ??????"> 
</DataTemplate> 

由於IsEnabled屬性不能在TimeSheetDetails中查到,但是可以在我的ViewModel中找到,我想直接綁定到我的視圖模型的屬性,但是當我嘗試從我的DataTemplate結合,我的ViewModel,它只似乎看我的TimeSheetDetail。

如何直接訪問我的ViewModel的公共屬性?

+0

我同樣的問題,檢查了這一點: [http://stackoverflow.com/questions/6814020/silverlight-databinding-error] – saber

回答

2

您可以綁定到你父母的DataContext

{Binding DataContext.IsEnabled, 
     RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}} 
+0

泰很多,真的有用,但你犯了一個小錯誤,它更像IsEnabled =「{Binding DataContext.IsEnabled,RelativeSource = {RelativeSource FindAncestor,AncestorType = ItemsControl}}」,因爲TimeSheet alredy是DataContext。 – Gab