Code WPF Demo
Code WPF Demo
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SinhVienDB.Models;
namespace SinhVienDB
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public class BooleanToGenderConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
if (value is bool isMale)
{
return isMale ? "Male" : "Female";
}
return value;
}
public MainWindow()
{
InitializeComponent();
_context = new UniversityDBContext();
Load_data();
}
public void Load_data()
{
ListView.ItemsSource=_context.Students.ToList();
cboMajor.ItemsSource=_context.Majors.ToList();
}
}
}
}
Code XAML
<Window x:Class="SinhVienDB.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SinhVienDB"
mc:Ignorable="d"
Title="Sinh Vien" Height="450" Width="700">
<Window.Resources>
<local:BooleanToGenderConverter x:Key="BooleanToGenderConverter" />
<local:intToName x:Key="intToName"/>
</Window.Resources>
<Grid Background="Aqua">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition
Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition
Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/> <ColumnDefinition
Width="Auto"/>
<ColumnDefinition Width="Auto"/> <ColumnDefinition
Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Margin="4 4 0 0" Content="Id: "/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="4 4 0 0" Name="txtId"
Width="150" IsReadOnly="true"
Text="{Binding Id , Mode = OneWay}" DataContext="{Binding SelectedItem
, ElementName= ListView}" />
<Label Grid.Row="0" Grid.Column="2" Margin="4 4 0 0" Content="Name: "/>
<TextBox Grid.Row="0" Grid.Column="3" Margin="4 4 0 0" Name="txtName"
Width="250"
Text="{Binding Name , Mode = OneWay}" DataContext="{Binding SelectedItem ,
ElementName= ListView}" />