06 March 2017

Storage / Data: DataBinding Collection trên Windows Store Apps (C #)

Yêu cầu: Windows 10 và Visual Studio 2015
- DataBinding: Collection hiển thị qua DataTemplate x:Key="Mau1"
<Grid.Resources>
    <DataTemplate x:Key="Mau1">
        <StackPanel>
            <TextBlock>//Show data</TextBlock>
        </StackPanel>
    </DataTemplate>
</Grid.Resources>
<StackPanel>
    <ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource Mau1}"></ItemsControl>
    <ListView ItemsSource="{Binding}" ItemTemplate="{StaticResource Mau1}"></ListView>
</StackPanel>
- Example : 
MainPage.xaml
Windows Store 2017
<Page
    x:Class="Theory3.BindingToCollection"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Theory3"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.Resources>
            <DataTemplate x:Key="Mau1">
                <StackPanel>
                    <TextBlock Text="{Binding Manufacturer}" FontSize="24"></TextBlock>
                    <TextBlock Text="{Binding Model}" FontSize="24"></TextBlock>
                    <TextBlock Text="{Binding Color}" FontSize="24"></TextBlock>
                    <TextBlock Text="{Binding Year}" FontSize="24"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>
        <StackPanel>
            <ItemsControl Name="ListCar" ItemsSource="{Binding}" ItemTemplate="{StaticResource Mau1}">

            </ItemsControl>
            <ListView Name="lvwCar" ItemsSource="{Binding}" ItemTemplate="{StaticResource Mau1}"></ListView>
        </StackPanel>

    </Grid>
</Page>
CarModel.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Theory3
{
    class CarModel
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _manufacturer;
        private string _model;
        private string _color;
        private int _year;

        public string Manufacturer
        {
            get { return _manufacturer; }
            set
            {
                _manufacturer = value;
                NotifyPropertyChanged("Manufacturer");
            }
        }

        public string Model
        {
            get { return _model; }
            set
            {
                _model = value;
                NotifyPropertyChanged("Model");
            }
        }

        public string Color
        {
            get { return _color; }
            set
            {
                _color = value;
                NotifyPropertyChanged("Color");
            }
        }

        public int Year
        {
            get { return _year; }
            set
            {
                _year = value;
                NotifyPropertyChanged("Year");
            }
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

}
MainPage.xaml.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Theory3
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class BindingToCollection : Page
    {
        public BindingToCollection()
        {
            this.InitializeComponent();
            List<CarModel> listCars = new List<CarModel>();
            listCars.Add(new CarModel()
            {
                Manufacturer = "Toyota",
                Model = "Vios",
                Color = "Black",
                Year = 2015
            });

            listCars.Add(new CarModel()
            {
                Manufacturer = "Mazda",
                Model = "CX-5",
                Color = "White",
                Year = 2016
            });
            listCars.Add(new CarModel()
            {
                Manufacturer = "Ferrari",
                Model = "CX-6",
                Color = "Black",
                Year = 2017
            });
            ListCar.DataContext = listCars;
            lvwCar.DataContext = listCars;

        }
    }
}
- Chạy ứng dụng

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang