06 March 2017

Controls: ListView hiển thị một danh sách trên Windows Store Apps (C #)

Yêu cầu: Windows 10 và Visual Studio 2015
- ListView Hiển thị danh sách dữ liệu
<ListView x:Name="name"></ListView>
- Example 1: Show ListView và Click
MainPage.xaml
Windows Store 2017
<Page
    x:Class="ListView.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ListView"
    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}">
        <ListView x:Name="lsView" SelectionChanged="ListView_SelectionChanged" Margin="42,33,675,441" FontSize="36" Background="DarkCyan">
            <x:String>Item 1</x:String>
            <x:String>Item 2</x:String>
            <x:String>Item 3</x:String>
        </ListView>
    </Grid>
</Page>
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.Popups;
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 ListView
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageDialog msgDialog = new MessageDialog("Your Selected : " + lsView.SelectedItem.ToString(), "Result");
            msgDialog.ShowAsync();
        }
    }
}
- Chạy chương trình
 - Click Item 1
 - Show MessageDialog

-Example 2: Hiển thị danh sách khung hình
MainPage.xaml
Windows Store 2017
<Page
    x:Class="ListView.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ListView"
    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}">
        <ListView x:Name="lsView"  Margin="42,33,675,10" FontSize="36" Background="DarkCyan">
            <Border Height="160" Width="200" Margin="5" Background="Blue" />
            <Border Height="160" Width="200" Margin="5" Background="Green" />
            <Border Height="160" Width="200" Margin="5" Background="Red" />
            <Border Height="160" Width="200" Margin="5" Background="Yellow" />
            <Border Height="160" Width="200" Margin="5" Background="Cyan" />
            <Border Height="160" Width="200" Margin="5" Background="Magenta" />
            <Border Height="160" Width="200" Margin="5" Background="DarkKhaki" />
            <Border Height="160" Width="200" Margin="5" Background="Beige" />
            <Border Height="160" Width="200" Margin="5" Background="DarkViolet" />
        </ListView>
    </Grid>
</Page>
- Chạy chương trình
 - Có thể bắt sự kiện click và cuộn trang

- Example 3: Đưa giá trị vào List Object và hiển thị
MainPage.xaml
Windows Store 2017
<Page
    x:Class="ListView.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ListView"
    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}">
        <ListView x:Name="lsView" SelectionChanged="ListView_SelectionChanged" Margin="42,33,675,441" FontSize="36" Background="DarkCyan">
            //Để trống
        </ListView>
    </Grid>
</Page>
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.Popups;
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 ListView
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            List<string> item = new List<string>();
            item.Add("Item 1");
            item.Add("Item 2");
            item.Add("Item 3");

            lsView.ItemsSource = item;
        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageDialog msgDialog = new MessageDialog("Your Selected : " + lsView.SelectedItem.ToString(), "Result");
            msgDialog.ShowAsync();
        }
    }
}
-Chạy chương trình
- Click Item 2 Show MessageDialog

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang