【问题标题】:Specifying a property of an object to use in ItemsControl binding指定要在 ItemsControl 绑定中使用的对象的属性
【发布时间】:2012-08-02 20:19:23
【问题描述】:

在我的示例 WP7 应用程序中,我想使用相同的数据模板在所有 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”?

【问题讨论】:

    标签: silverlight xaml binding


    【解决方案1】:

    您可以使用内容模板:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-16
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多