22 March 2017

Controls: Bắt sự kiện Event Click lấy giá trị và hiển thị với ListView vs ScrollViewer Windows Store

MainPage.xaml
Windows Store 2017
<Page
    x:Class="DetailsView.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DetailsView"
    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.ColumnDefinitions>
            <ColumnDefinition Width="665*"></ColumnDefinition>
            <ColumnDefinition Width="701*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <ListView x:Name="setValueEventClick" Margin="100,100,110,273" Grid.Column="0" Grid.Row="0" Width="auto" Height="auto" Background="DarkCyan">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" FontSize="24"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        //ScrollViewer Hien thi gia tri khi click xong
        <ScrollViewer x:Name="EmployeeDetails" Grid.Row="0" Margin="0,100,216,273" Grid.Column="1" Background="DarkCyan">
            <StackPanel DataContext="{Binding SelectedItem,ElementName=setValueEventClick}">
                <StackPanel.Resources>
                    <local:DateConverter x:Key="newDate"></local:DateConverter>
                </StackPanel.Resources>
                <StackPanel>
                    <TextBlock Text="{Binding Name}" FontSize="24" SelectionHighlightColor="White"></TextBlock>
                </StackPanel>
                <StackPanel>
                    <TextBlock Text="{Binding Address}" FontSize="24"></TextBlock>
                </StackPanel>
                <StackPanel>
                    <TextBlock Text="{Binding Doj,Converter={StaticResource newDate}}" FontSize="24"></TextBlock>
                </StackPanel>
            </StackPanel>
        </ScrollViewer>

    </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.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;

namespace DetailsView
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            List<Employee> listEmps = new List<Employee>() {
                new Employee() {
                    Name="Mr Antonio",
                    Address="Indonesia",
                    Doj=new DateTime(1993,4,3)
                },
                new Employee() {
                    Name="Ms Sophia",
                    Address="American",
                    Doj=new DateTime(1982,4,9)
                },
                new Employee() {
                    Name="Ms Lisa",
                    Address="Europe",
                    Doj=new DateTime(1987,12,19)
                }
            };
            setValueEventClick.ItemsSource = listEmps;
        }
    }
}
Class Convert dữ liệu Date Time Windows Store
DateConverter.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;

namespace DetailsView
{
    public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            DateTime data = (DateTime)value;
            return data.Day + "/" + data.Month + "/" + data.Year;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang