2012-08-05 71 views
2

在我的例子WP7應用程序我想使用相同的數據模板中的所有的ItemsControl控件來顯示結果。指定對象的屬性在ItemsControl中使用綁定

<DataTemplate x:Key="fruitDetails" > 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock Text="{Binding Name}" /> 
     <TextBlock Text="{Binding Color}" /> 
    </StackPanel > 
</DataTemplate> 

因爲我可以綁定像這種水果的清單:

XAML: 
<ItemsControl Name="itemControls1" ItemTemplate="{StaticResource fruitDetails }" > 

C# 
ObservableCollection<Fruit> fruits = new ObservableCollection <Fruit>(); 
itemControls1.ItemSource = fruits; 

但我的一些名單的有水果的性質:

class CargoBox 
{ 
    public int CargoBoxNumber { get; set; } 
    public Fruit TypeOfFruit { get; set; } 
} 

ObservableCollection <CargoBox> boxes = new ObservableCollection <CargoBox>(); 

如果我想要顯示的水果使用相同的DataTemplate中,我該怎麼綁定這個列表,並指定屬性「TypeOfFruit」?

回答

1

你可以使用一個內容模板:

<StackPanel Orientation="Vertical"> 
    <TextBox Text="{Binding CargoBoxNumber}" /> 
    <ContentControl Content="{Binding TypeOfFruit}" ContentTemplate="{StaticResource fruitDetails}" /> 
</StackPanel> 
+0

這偉大工程! – BrainProxy 2012-08-05 20:10:44

相關問題