0% found this document useful (0 votes)
114 views4 pages

WPF Student Management 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views4 pages

WPF Student Management 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using System;

using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace SinhVienDB
{

/// <summary>
/// Interaction logic for [Link]
/// </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 = [Link](x => [Link] == MajorId).FirstOrDefault();
return [Link];
}
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()
{
[Link]=_context.[Link]();
[Link]=_context.[Link]();
}

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="[Link]"
xmlns="[Link]
xmlns:x="[Link]
xmlns:d="[Link]
xmlns:mc="[Link]
xmlns:local="clr-namespace:SinhVienDB"
mc:Ignorable="d"
Title="Sinh Vien" Height="450" Width="700">
<[Link]>
<local:BooleanToGenderConverter x:Key="BooleanToGenderConverter" />
<local:intToName x:Key="intToName"/>
</[Link]>
<Grid Background="Aqua">
<[Link]>
<RowDefinition Height="Auto"/> <RowDefinition
Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition
Height="Auto"/>
<RowDefinition Height="*"/>
</[Link]>
<[Link]>
<ColumnDefinition Width="Auto"/> <ColumnDefinition
Width="Auto"/>
<ColumnDefinition Width="Auto"/> <ColumnDefinition
Width="Auto"/>
</[Link]>
<Label [Link]="0" [Link]="0" Margin="4 4 0 0" Content="Id: "/>
<TextBox [Link]="0" [Link]="1" Margin="4 4 0 0" Name="txtId"
Width="150" IsReadOnly="true"
Text="{Binding Id , Mode = OneWay}" DataContext="{Binding SelectedItem
, ElementName= ListView}" />
<Label [Link]="0" [Link]="2" Margin="4 4 0 0" Content="Name: "/>
<TextBox [Link]="0" [Link]="3" Margin="4 4 0 0" Name="txtName"
Width="250"
Text="{Binding Name , Mode = OneWay}" DataContext="{Binding SelectedItem ,
ElementName= ListView}" />

<Label [Link]="1" [Link]="0" Margin="4 4 0 0" Content="Dob: "/>


<DatePicker [Link]="1" [Link]="1" Margin="4 4 0 0" Name="dtDoB"
Width="150"
SelectedDate="{Binding Dob, Mode = OneWay}" DataContext="{Binding
SelectedItem , ElementName= ListView}" />

<Label [Link]="1" [Link]="2" Margin="4 4 0 0" Content="Gender: "/>


<RadioButton [Link]="1" [Link]="3" Margin="4 4 0 0" Name="rdomale"
Content="Male"
IsChecked="{Binding Gender, Mode = OneWay}" DataContext="{Binding
SelectedItem , ElementName= ListView}"
/>

<RadioButton [Link]="1" [Link]="3" Margin="80 4 0 0"


Name="rdofemale" Content="FeMale"
IsChecked="{Binding !Gender, Mode = OneWay}" DataContext="{Binding
SelectedItem , ElementName= ListView}"
/>
<Label [Link]="2" [Link]="0" Margin="4 4 0 0" Content="Phone: "/>
<TextBox [Link]="2" [Link]="1" Margin="4 4 0 0" Name="txtPhone"
Width="150"
Text="{Binding Phone , Mode = OneWay}" DataContext="{Binding SelectedItem ,
ElementName= ListView}" />
<Label [Link]="2" [Link]="2" Margin="4 4 0 0" Content="Major: "/>
<ComboBox [Link]="2" [Link]="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 [Link]="3" Width="100" [Link]="4"
HorizontalAlignment="Left" x:Name="Load" Content="Load Data" Margin="5 5 0 0"
Click="Load_Data"/>
<Button [Link]="3" Width="100" [Link]="4"
HorizontalAlignment="Left" x:Name="Add" Content="Create" Margin="150 5 0 0"
Click="Add_Data"/>
<Button [Link]="3" Width="100" [Link]="4"
HorizontalAlignment="Left" x:Name="Update" Content="Update" Margin="300 5 0 0"
Click="Update_Data"/>
<Button [Link]="3" Width="100" [Link]="4"
HorizontalAlignment="Left" x:Name="Delete" Content="Delete" Margin="450 5 0 0"
Click="Delete_Data"/>
<DataGrid [Link]="4" [Link]="4" Margin="4 4 4 4"
AutoGenerateColumns="False" CanUserResizeColumns="True"
Name="ListView"
MouseLeftButtonUp="Student_Up"
SelectionChanged="ListView_SelectionChanged">
<[Link]>
<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}}"/>
</[Link]>
</DataGrid>
</Grid>
</Window>

Common questions

Powered by AI

The application leverages data binding and event handlers like the ListView_SelectionChanged method to support dynamic updates upon selection changes . The DataContext binding updates display fields to reflect the currently selected record in the ListView, offering immediate feedback and allowing for intuitive modifications by the user . However, lacking explicit logic for ListView_SelectionChanged leaves potential gaps in handling more complex interactions, potentially requiring further coding to handle edge cases and data integrity across UI updates robustly.

The grid layout in the XAML code divides the UI into a structured matrix of rows and columns, allowing precise placement and alignment of UI components like labels, text boxes, and buttons . This flexibility supports responsive design by enabling components to adapt dynamically to different window sizes or resolutions. It provides consistency and neatness in layout, contributing to an intuitive visual hierarchy and enhancing user experience .

The use of DataContext in the XAML code binds UI elements to properties based on the SelectedItem of the ListView, enabling display synchronization with currently selected data records. While this method simplifies data binding and ensures consistent UI updates , potential issues include performance overhead with frequent DataContext changes and complexities in handling nested bindings that could lead to UI latency or incorrect data displays . Proper management strategies and efficient update handling could mitigate these concerns.

The XAML code defines UI components like Labels, TextBoxes, DatePickers, RadioButtons, ComboBox, and Buttons arranged in a Grid layout with specified Row and Column definitions . Data binding is extensively used; elements bind to properties of the SelectedItem in ListView using OneWay mode, allowing UI components to display currently selected records . The ComboBox bound to a property in the ListView uses the intToName converter to show major names instead of IDs, enhancing user experience .

Data loading is initiated in the MainWindow constructor by calling the Load_data method, which assigns the output of _context.Students.ToList() to ListView.ItemsSource and _context.Majors.ToList() to cboMajor.ItemsSource . The setup uses Entity Framework to query database tables, ensuring efficiency and flexibility for handling large datasets. Extensibility is achieved through separate methods for CRUD operations, although their bodies must be implemented to conduct transactions or interact with the UI meaningfully beyond loading data .

The intToName class, implementing the IValueConverter interface, translates a MajorId integer from the database into its corresponding major name string. It interacts with UniversityDBContext by querying the Majors table for a record matching the given MajorId and returning the Name field of that major . This class facilitates displaying user-friendly major names in the application UI instead of raw numerical IDs .

The similarly named Load_Data and Load_data methods might lead to confusion regarding their specific roles and functionalities, impacting code readability and maintainability . Despite current code where only Load_data is implemented for setting the ListView and cboMajor's ItemsSource properties , potential future use or additional functionality of Load_Data could cause misunderstanding without distinct and descriptive naming conventions, increasing the likelihood of maintenance errors.

Converters like BooleanToGenderConverter and intToName are applied in data bindings to transform data into a more user-friendly format. BooleanToGenderConverter changes boolean gender data into 'Male' or 'Female', while intToName translates MajorId integers into their respective major names . These converters enhance UI presentation by making the displayed data more understandable and applicable to typical usage contexts without requiring users to interpret raw database values, improving usability and accessibility .

The BooleanToGenderConverter class implements the IValueConverter interface, primarily converting a boolean value to a gender string ('Male' if true, 'Female' if false) in the Convert method . However, it does not support converting back from string to boolean, as indicated by the NotSupportedException thrown in the ConvertBack method, thereby limiting its use to one-way conversions only .

The CRUD operations in the WPF application include data loading, creation, updating, and deletion, triggered by Button click events and involving interaction with the UniversityDBContext . However, specific method implementations for Add_Data, Update_Data, and Delete_Data functions are not detailed, indicating that further coding is needed for these functionalities to modify data records and affect the ListView display. Also, error handling and validation are not explicitly described, which might limit real-world usability .

You might also like