-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooksWindow.xaml
More file actions
330 lines (293 loc) · 13.4 KB
/
BooksWindow.xaml
File metadata and controls
330 lines (293 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<Window x:Class="DatabaseExampleWPF.BooksWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Manage Books"
Height="600"
Width="900"
WindowStartupLocation="CenterScreen">
<!--
This window demonstrates CRUD operations for the Books table
Also shows many-to-many relationship management with Authors through BookAuthors table
-->
<Window.Resources>
<!-- Button style for this window -->
<Style TargetType="Button">
<Setter Property="Padding" Value="10,5"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Cursor" Value="Hand"/>
</Style>
<!-- TextBlock style -->
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="5,5,5,2"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<!-- TextBox style -->
<Style TargetType="TextBox">
<Setter Property="Padding" Value="5"/>
<Setter Property="Margin" Value="5,2,5,5"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</Window.Resources>
<!--
Main Grid with two columns:
- Left: DataGrid showing all books
- Right: Form for add/edit operations and author management
-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<!-- Left column: takes proportional space -->
<ColumnDefinition Width="350"/>
<!-- Right column: fixed width for form -->
</Grid.ColumnDefinitions>
<!-- LEFT SIDE: Books List -->
<Grid Grid.Column="0" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<!-- Header -->
<RowDefinition Height="*"/>
<!-- DataGrid -->
<RowDefinition Height="Auto"/>
<!-- Action buttons -->
</Grid.RowDefinitions>
<!-- Header -->
<TextBlock Grid.Row="0"
Text="Books List"
FontSize="18"
FontWeight="Bold"
Margin="5,5,5,10"/>
<!--
DataGrid: Displays data in a table format
This is one of the most important WPF controls for displaying database records
Properties explained:
- x:Name: Name to reference in code
- AutoGenerateColumns="False": We define columns manually for control
- IsReadOnly="True": Users can't edit directly in grid
- SelectionMode="Single": Only one row can be selected at a time
- CanUserAddRows/DeleteRows="False": Disable inline add/delete
- GridLinesVisibility: Show horizontal lines between rows
- AlternatingRowBackground: Different color for every other row (easier to read)
- SelectionChanged: Event when user selects a different row
-->
<DataGrid Grid.Row="1"
x:Name="dgBooks"
AutoGenerateColumns="False"
IsReadOnly="True"
SelectionMode="Single"
CanUserAddRows="False"
CanUserDeleteRows="False"
GridLinesVisibility="Horizontal"
AlternatingRowBackground="#F5F5F5"
SelectionChanged="DgBooks_SelectionChanged"
Margin="5">
<!--
DataGrid.Columns: Define what columns to display
Each column is bound to a property of the Book object
-->
<DataGrid.Columns>
<!--
DataGridTextColumn: Displays text data
Binding: Specifies which property of the Book object to display
Header: Column header text
Width: Column width (Auto, *, or specific value)
-->
<DataGridTextColumn Header="ID"
Binding="{Binding BookID}"
Width="50"/>
<DataGridTextColumn Header="Title"
Binding="{Binding Title}"
Width="*"/>
<!-- Width="*" means take remaining space -->
<DataGridTextColumn Header="ISBN"
Binding="{Binding ISBN}"
Width="120"/>
<DataGridTextColumn Header="Year"
Binding="{Binding YearPublished}"
Width="70"/>
</DataGrid.Columns>
</DataGrid>
<!-- Action Buttons -->
<StackPanel Grid.Row="2"
Orientation="Horizontal"
HorizontalAlignment="Center"
Margin="5">
<!-- Orientation="Horizontal": Stack buttons side by side -->
<Button x:Name="btnRefresh"
Content="🔄 Refresh List"
Click="BtnRefresh_Click"
Background="#2196F3"
Foreground="White"/>
<Button x:Name="btnDelete"
Content="🗑️ Delete Selected Book"
Click="BtnDelete_Click"
Background="#F44336"
Foreground="White"/>
</StackPanel>
</Grid>
<!-- RIGHT SIDE: Add/Edit Form and Author Management -->
<Grid Grid.Column="1"
Margin="10"
Background="#F9F9F9">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<!-- Form header -->
<RowDefinition Height="Auto"/>
<!-- Book form -->
<RowDefinition Height="*"/>
<!-- Author management -->
</Grid.RowDefinitions>
<!-- Form Header -->
<Border Grid.Row="0"
Background="#1976D2"
Padding="10"
Margin="0,0,0,10">
<TextBlock x:Name="txtFormTitle"
Text="Add New Book"
FontSize="16"
FontWeight="Bold"
Foreground="White"
Margin="0"/>
</Border>
<!-- Book Entry Form -->
<Border Grid.Row="1"
Background="White"
Padding="10"
Margin="0,0,0,10"
BorderBrush="#DDDDDD"
BorderThickness="1">
<StackPanel>
<!--
Each field follows the pattern:
- Label (TextBlock)
- Input control (TextBox)
This is a standard form layout in WPF
-->
<TextBlock Text="Book Title *"/>
<TextBox x:Name="txtTitle"
MaxLength="200"/>
<!-- MaxLength: Limits how many characters can be entered -->
<TextBlock Text="ISBN"/>
<TextBox x:Name="txtISBN"
MaxLength="20"/>
<TextBlock Text="Year Published *"/>
<TextBox x:Name="txtYear"/>
<!-- Action Buttons -->
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
Margin="0,15,0,0">
<Button x:Name="btnSave"
Content="💾 Save Book"
Click="BtnSave_Click"
Background="#4CAF50"
Foreground="White"
Width="110"/>
<Button x:Name="btnClear"
Content="🔄 Clear Form"
Click="BtnClear_Click"
Background="#FF9800"
Foreground="White"
Width="110"/>
</StackPanel>
<!-- Info text -->
<TextBlock Text="* Required fields"
FontSize="10"
FontStyle="Italic"
Foreground="Gray"
HorizontalAlignment="Center"
Margin="0,10,0,0"/>
</StackPanel>
</Border>
<!-- Author Management Section -->
<!--
This section demonstrates managing the many-to-many relationship
between Books and Authors through the BookAuthors junction table
-->
<Border Grid.Row="2"
Background="White"
Padding="10"
BorderBrush="#DDDDDD"
BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Section Header -->
<TextBlock Grid.Row="0"
Text="Manage Authors for Selected Book"
FontSize="14"
FontWeight="Bold"
Margin="5,5,5,10"/>
<!--
ListBox: Displays a list of items
Used here to show authors assigned to the selected book
-->
<ListBox Grid.Row="1"
x:Name="lstAssignedAuthors"
Margin="5"
MinHeight="100">
<!--
ItemTemplate: Defines how each item is displayed
DataTemplate: Template for the visual representation
-->
<ListBox.ItemTemplate>
<DataTemplate>
<!--
Binding to FullName property of Author object
Each item will display the author's full name
-->
<TextBlock Text="{Binding FullName}"
Margin="2"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- Add Author Section -->
<StackPanel Grid.Row="2" Margin="5,10,5,5">
<TextBlock Text="Add Author to Book:"/>
<!--
ComboBox: Dropdown list
Used to select an author to add to the book
DisplayMemberPath: Which property to show in dropdown
SelectedValuePath: Which property to use as the value
-->
<ComboBox x:Name="cboAvailableAuthors"
DisplayMemberPath="FullName"
SelectedValuePath="AuthorID"
Margin="5,2,5,5"/>
<Button x:Name="btnAddAuthor"
Content="➕ Add Author to Book"
Click="BtnAddAuthor_Click"
Background="#4CAF50"
Foreground="White"
HorizontalAlignment="Stretch"/>
</StackPanel>
<!-- Remove Author Button -->
<Button Grid.Row="3"
x:Name="btnRemoveAuthor"
Content="➖ Remove Selected Author from Book"
Click="BtnRemoveAuthor_Click"
Background="#F44336"
Foreground="White"
Margin="5"
HorizontalAlignment="Stretch"/>
<!--
Note TextBlock - shown when no book is selected
This will be made visible/hidden in code-behind
-->
<TextBlock Grid.Row="0" Grid.RowSpan="4"
x:Name="txtNoBookSelected"
Text="Select a book from the list to manage its authors"
TextWrapping="Wrap"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="Gray"
FontStyle="Italic"
Margin="20"/>
</Grid>
</Border>
</Grid>
</Grid>
</Window>