Windows Phone 8.1 Binding 数据绑定

<ListView x:Name="listBox">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Background="Gray" Width="450" Margin="10">
                <!--绑定Food类的IconUri属性-->
                <Image Source="{Binding IconUri}" Stretch="None"/>
                <!--绑定Food类的Name属性-->
                <TextBlock Text="{Binding Name}" FontSize="40" Width="150"/>
                <!--绑定Food类的Description属性-->
                <TextBlock Text="{Binding Description}" FontSize="20" Width="280"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
public sealed partial class MainPage: Page {
    public List < Food > AllFood {
        get;
        set;
    }
    public MainPage() {
        this.InitializeComponent();
        AllFood = new List < Food > ();
        Food item0 = new Food() {
            Name = "西红柿",
            IconUri = "Images/Tomato.png",
            Type = "Healthy",
            Description = "西红柿的味道不错。"
        };
        Food item1 = new Food() {
            Name = "茄子",
            IconUri = "Images/Beer.png",
            Type = "NotDetermined",
            Description = "不知道这个是否好吃。"
        };
        Food item2 = new Food() {
            Name = "火腿",
            IconUri = "Images/fries.png",
            Type = "Unhealthy",
            Description = "这是不健康的食品。"
        };
        Food item3 = new Food() {
            Name = "三明治",
            IconUri = "Images/Hamburger.png",
            Type = "Unhealthy",
            Description = "肯德基的好吃?"
        };
        Food item4 = new Food() {
            Name = "冰激凌",
            IconUri = "Images/icecream.png",
            Type = "Healthy",
            Description = "给小朋友吃的。"
        };
        Food item5 = new Food() {
            Name = "Pizza",
            IconUri = "Images/Pizza.png",
            Type = "Unhealthy",
            Description = "这个非常不错。"
        };
        Food item6 = new Food() {
            Name = "辣椒",
            IconUri = "Images/Pepper.png",
            Type = "Healthy",
            Description = "我不喜欢吃这东西。"
        };
        AllFood.Add(item0);
        AllFood.Add(item1);
        AllFood.Add(item2);
        AllFood.Add(item3);
        AllFood.Add(item4);
        AllFood.Add(item5);
        AllFood.Add(item6);
        listBox.ItemsSource = AllFood;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e) {}
}

public class Food {
    public string Name {
        get;
        set;
    }

    public string Description {
        get;
        set;
    }

    public string IconUri {
        get;
        set;
    }

    public string Type {
        get;
        set;
    }

}

网友评论

无吐槽,不精彩,期待您的参与。

吐槽几句

"(必填)"

"(选填)"

"(必填,不公开)"

(必填,最多100个字符。)

先说这么多吧