2013-06-19 36 views
4

當我把一個DataTemplate<Page.Resources>部分,編輯設計師裏面綁定時,它繼承了PageDataContext。然而,在運行時DataTemplate正在使用的元件,其具有其自身的DataContext內部Page。我想設計師展示內DataContext而結合代替。設計時的DataContext在一個DataTemplate?

是否有類似d:DataContext的標籤爲DataTemplates?設置DataType沒有做任何事情。

回答

4

我發現你可以在DataTemplate的根元素上設置d:DataContext

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

<ListBox ItemsSource="{Binding People}"> 
<ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel d:DataContext="{d:DesignInstance Type=local:Person}"> 
      <TextBlock Text="{Binding Name}" /> 
      <TextBlock Text="{Binding Age}" /> 
      <TextBlock Text="{Binding Height}" /> 
     </StackPanel> 
    </DataTemplate> 
</ListBox.ItemTemplate> 

1

我意識到這是超舊的,但製作SO更好的緣故。的DataTemplates有通過數據類型標籤「的DataContext」。

<DataTemplate DataType="{x:Type local:Person}"> 
    <StackPanel> 
     <TextBlock Text="{Binding Name}" /> 
     <TextBlock Text="{Binding Age}" /> 
     <TextBlock Text="{Binding Height}" /> 
    </StackPanel> 
</DataTemplate> 
相關問題