05 March 2017

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

Yêu cầu: Windows 10 và Visual Studio 2015
- ComboBox: Danh sách dữ liệu.
<ComboBox x:Name="cbBox"> </ComboBox> 
- Example 1: Show ComboBox và Click

MainPage.xaml
Windows Store 2017
<Page
    x:Class="ListBox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ListBox"
    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}">
        <ComboBox x:Name="cbBox" Height="50" HorizontalAlignment="Left" Margin="210,55,0,0"  VerticalAlignment="Top" Width="470" SelectionChanged="ComboBox_SelectionChanged">
            <x:String>Item 1</x:String>
            <x:String>Item 2</x:String>
            <x:String>Item 3</x:String>
        </ComboBox>
    </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 ListBox
{
    /// <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 ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageDialog msgDialog = new MessageDialog("Your Selected : " + cbBox.SelectedItem.ToString(), "Result");
            msgDialog.ShowAsync();
        }
    }
}
- Click Item 1
- Show MessageDialog
- Example 2: Đưa giá trị vào List Object và hiển thị

MainPage.xaml
Windows Store 2017
<Page
    x:Class="ListBox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ListBox"
    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}">
        <ComboBox x:Name="cbBox" Height="50" HorizontalAlignment="Left" Margin="210,55,0,0"  VerticalAlignment="Top" Width="470" SelectionChanged="ComboBox_SelectionChanged">
            //Để trống
        </ComboBox>
    </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 ListBox
{
    /// <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");

            cbBox.ItemsSource = item;
        }
        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageDialog msgDialog = new MessageDialog("Your Selected : " + cbBox.SelectedItem.ToString(), "Result");
            msgDialog.ShowAsync();
        }
    }
}
- Màn hình Design
 - Click Item 1
 - Show MessageDialog

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang