0% found this document useful (0 votes)
2 views

Code WPF Demo

The document contains C# code for a WPF application that manages student data, including gender and major information. It defines value converters for gender and major name conversion, and implements a main window with data binding to display and manipulate student records. The XAML layout includes controls for displaying student details and buttons for loading, adding, updating, and deleting records.

Uploaded by

Kiệt Phạm
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Code WPF Demo

The document contains C# code for a WPF application that manages student data, including gender and major information. It defines value converters for gender and major name conversion, and implements a main window with data binding to display and manipulate student records. The XAML layout includes controls for displaying student details and buttons for loading, adding, updating, and deleting records.

Uploaded by

Kiệt Phạm
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

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 object ConvertBack(object value, Type targetType, object parameter,


CultureInfo culture)
{
throw new NotSupportedException();
}
}

public class intToName : IValueConverter


{
UniversityDBContext db = new UniversityDBContext();
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
if (value is int MajorId)
{
var tmp = db.Majors.Where(x => x.Id == MajorId).FirstOrDefault();
return tmp.Name;
}
return value;
}

public object ConvertBack(object value, Type targetType, object parameter,


CultureInfo culture)
{
throw new NotSupportedException();
}
}
public partial class MainWindow : Window
{
private readonly UniversityDBContext _context;

public MainWindow()
{
InitializeComponent();
_context = new UniversityDBContext();
Load_data();
}
public void Load_data()
{
ListView.ItemsSource=_context.Students.ToList();
cboMajor.ItemsSource=_context.Majors.ToList();
}

private void Load_Data(object sender, RoutedEventArgs e)


{

private void Add_Data(object sender, RoutedEventArgs e)


{

private void Update_Data(object sender, RoutedEventArgs e)


{

private void Delete_Data(object sender, RoutedEventArgs e)


{

private void ListView_SelectionChanged(object sender,


SelectionChangedEventArgs e)
{

private void Student_Up(object sender, MouseButtonEventArgs e)


{

}
}
}

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}" />

<Label Grid.Row="1" Grid.Column="0" Margin="4 4 0 0" Content="Dob: "/>


<DatePicker Grid.Row="1" Grid.Column="1" Margin="4 4 0 0" Name="dtDoB"
Width="150"
SelectedDate="{Binding Dob, Mode = OneWay}" DataContext="{Binding
SelectedItem , ElementName= ListView}" />

<Label Grid.Row="1" Grid.Column="2" Margin="4 4 0 0" Content="Gender: "/>


<RadioButton Grid.Row="1" Grid.Column="3" Margin="4 4 0 0" Name="rdomale"
Content="Male"
IsChecked="{Binding Gender, Mode = OneWay}" DataContext="{Binding
SelectedItem , ElementName= ListView}"
/>

<RadioButton Grid.Row="1" Grid.Column="3" Margin="80 4 0 0"


Name="rdofemale" Content="FeMale"
IsChecked="{Binding !Gender, Mode = OneWay}" DataContext="{Binding
SelectedItem , ElementName= ListView}"
/>
<Label Grid.Row="2" Grid.Column="0" Margin="4 4 0 0" Content="Phone: "/>
<TextBox Grid.Row="2" Grid.Column="1" Margin="4 4 0 0" Name="txtPhone"
Width="150"
Text="{Binding Phone , Mode = OneWay}" DataContext="{Binding SelectedItem ,
ElementName= ListView}" />
<Label Grid.Row="2" Grid.Column="2" Margin="4 4 0 0" Content="Major: "/>
<ComboBox Grid.Row="2" Grid.Column="3" Margin="0 4 0 0" Name="cboMajor"
Width="150"
ItemsSource="{Binding Major, Mode=OneWay }"
DisplayMemberPath="Name"
SelectedValuePath="Id"
SelectedValue="{Binding Id }" DataContext="{Binding SelectedItem,
ElementName= ListView}"
/>
<Button Grid.Row="3" Width="100" Grid.ColumnSpan="4"
HorizontalAlignment="Left" x:Name="Load" Content="Load Data" Margin="5 5 0 0"
Click="Load_Data"/>
<Button Grid.Row="3" Width="100" Grid.ColumnSpan="4"
HorizontalAlignment="Left" x:Name="Add" Content="Create" Margin="150 5 0 0"
Click="Add_Data"/>
<Button Grid.Row="3" Width="100" Grid.ColumnSpan="4"
HorizontalAlignment="Left" x:Name="Update" Content="Update" Margin="300 5 0 0"
Click="Update_Data"/>
<Button Grid.Row="3" Width="100" Grid.ColumnSpan="4"
HorizontalAlignment="Left" x:Name="Delete" Content="Delete" Margin="450 5 0 0"
Click="Delete_Data"/>
<DataGrid Grid.Row="4" Grid.ColumnSpan="4" Margin="4 4 4 4"
AutoGenerateColumns="False" CanUserResizeColumns="True"
Name="ListView"
MouseLeftButtonUp="Student_Up"
SelectionChanged="ListView_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Width="50" Header="Id" Binding="{Binding
Path=Id}"/>
<DataGridTextColumn Width="150" Header="Name" Binding="{Binding
Path=Name}"/>
<DataGridTextColumn Width="150" Header="DateofBird"
Binding="{Binding Path=Dob, StringFormat=dd/MM/yyyy}"/>
<DataGridTextColumn Width="60" Header="Gender" Binding="{Binding
Gender, Converter={StaticResource BooleanToGenderConverter}}"/>
<DataGridTextColumn Width="100" Header="Phone" Binding="{Binding
Path=Phone}"/>
<DataGridTextColumn Width="150" Header="Major" Binding="{Binding
Path=Major, Converter={StaticResource intToName}}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

You might also like