• Source
    1. <!--http://www.bravohex.com-->
    2. <Window x:Class="WPF_CRUD_EFCodeFirst_Simple.MainWindow"
    3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    5. Title="Wpf Example: CRUD EF Code First " Height="350
    6. " Width="600">
    7.  
    8. <Grid>
    9. <!--Chia Form thành 4 hàng-->
    10. <Grid.RowDefinitions>
    11. <RowDefinition Height="Auto"/>
    12. <RowDefinition Height="Auto"/>
    13. <RowDefinition Height="Auto"/>
    14. <RowDefinition Height="*"/>
    15. </Grid.RowDefinitions>
    16. <!--Hàng 1-->
    17. <Label Grid.Row="0" Content="DEMO" FontWeight="Bold" HorizontalAlignment="Center"/>
    18.  
    19. <!--Hàng 2-->
    20. <Grid Grid.Row="1">
    21. <!--Chia thành 2 hàng và 4 cột nhỏ-->
    22. <Grid.RowDefinitions>
    23. <RowDefinition Height="Auto"/>
    24. <RowDefinition Height="Auto"/>
    25. <RowDefinition Height="Auto"/>
    26. </Grid.RowDefinitions>
    27. <Grid.ColumnDefinitions>
    28. <ColumnDefinition Width="Auto"/>
    29. <ColumnDefinition Width="*"/>
    30. <ColumnDefinition Width="Auto"/>
    31. <ColumnDefinition Width="*"/>
    32. </Grid.ColumnDefinitions>
    33. <Label Grid.Row="0" Grid.Column="0" Content="Employee Id:"/>
    34. <Label Grid.Row="1" Grid.Column="0" Content="First Name:"/>
    35. <Label Grid.Row="1" Grid.Column="2" Content="Last Name:"/>
    36. <Label Grid.Row="2" Grid.Column="0" Content="Birthday:"/>
    37. <Label Grid.Row="2" Grid.Column="2" Content="Dep Id:"/>
    38.  
    39. <TextBox Name="TxtId" Grid.Row="0" Grid.Column="1" IsEnabled="False" Margin="5,5,5,5"
    40. Text="{Binding DataContext.CurrentSelectedEmployee.Id}" />
    41. <TextBox Name="TxtFirstName" Grid.Row="1" Grid.Column="1" Margin="5,5,5,5"
    42. Text="{Binding DataContext.CurrentSelectedEmployee.FirstName}" />
    43. <TextBox Name="TxtLastName" Grid.Row="1" Grid.Column="3" Margin="5,5,5,5"
    44. Text="{Binding DataContext.CurrentSelectedEmployee.LastName}" />
    45.  
    46. <DatePicker Name="DtpkBirthday" Grid.Row="2" Grid.Column="1" Margin="5,5,5,5"
    47. SelectedDate="{Binding DataContext.CurrentSelectedEmployee.Birthday}" />
    48.  
    49. <ComboBox Name="CbbDepartment" Grid.Row="2" Grid.Column="3" Margin="5,5,5,5"
    50. ItemsSource="{Binding DataContext.Departmentses}"
    51. SelectedValuePath="Id"
    52. DisplayMemberPath="Name"
    53. SelectedValue="{Binding DataContext.CurrentSelectedEmployee.DepId}" />
    54. </Grid>
    55.  
    56. <!--Hàng 3-->
    57. <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="0,10,0,10" HorizontalAlignment="Center">
    58. <Button Name="BtnInsert" Foreground="Teal" Content="&#xf0fe;" Width="70" Height="35" Margin="0,0,40,0" Background="Transparent" BorderThickness="0" FontSize="30" Style="{StaticResource FontAwesome}" Click="BtnInsert_Click"/>
    59. <Button Name="BtnEdit" Foreground="YellowGreen" Content="&#xf044;" Width="70" Height="35" Margin="0,0,20,0" Background="Transparent" BorderThickness="0" FontSize="30" Style="{StaticResource FontAwesome}" Click="BtnEdit_Click"/>
    60. <Button Name="BtnDelete" Foreground="Tomato" Content="&#xf014;" Width="70" Height="35" Margin="20,0,0,0" Background="Transparent" BorderThickness="0" FontSize="30" Style="{StaticResource FontAwesome}" Click="BtnDelete_Click"/>
    61. <Button Name="BtnExit" Content="&#xf011;" Width="70" Height="35" Margin="40,0,0,0" Background="Transparent" BorderThickness="0" FontSize="30" Style="{StaticResource FontAwesome}" Click="BtnExit_Click"/>
    62. </StackPanel>
    63.  
    64. <!--Hàng 4-->
    65. <DataGrid Name="DataGrid1" Grid.Row="3" Margin="10,0,10,10"
    66. RenderOptions.ClearTypeHint="Enabled"
    67. TextOptions.TextFormattingMode="Display"
    68. CanUserAddRows="False"
    69. CanUserDeleteRows="False"
    70. SelectionUnit="FullRow"
    71. SelectedItem="{Binding DataContext.CurrentSelectedEmployee, Mode=TwoWay}"
    72. ItemsSource="{Binding DataContext.Employees, Mode=TwoWay}"
    73. AutoGenerateColumns="false" >
    74.  
    75. <DataGrid.Columns>
    76. <!--Column 1: Employee Id-->
    77. <DataGridTextColumn Header="Emplyee Id" Binding="{Binding Id}"/>
    78.  
    79. <!--Column 2: First Name-->
    80. <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
    81.  
    82. <!--Column 3: Last Name-->
    83. <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
    84.  
    85. <!--Column 4: Birth Day-->
    86. <DataGridTemplateColumn Header="Birth Day" >
    87. <DataGridTemplateColumn.CellTemplate>
    88. <DataTemplate>
    89. <DatePicker SelectedDate="{Binding Birthday}" BorderThickness="0" />
    90. </DataTemplate>
    91. </DataGridTemplateColumn.CellTemplate>
    92. </DataGridTemplateColumn>
    93.  
    94. <!--Column 5: Department Id-->
    95. <DataGridTemplateColumn Header="Department" >
    96. <DataGridTemplateColumn.CellTemplate>
    97. <DataTemplate>
    98. <ComboBox ItemsSource="{Binding DataContext.Departmentses , RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
    99. DisplayMemberPath="Name" SelectedValuePath="Id" SelectedValue="{Binding DepId}" />
    100. </DataTemplate>
    101. </DataGridTemplateColumn.CellTemplate>
    102. </DataGridTemplateColumn>
    103.  
    104. </DataGrid.Columns>
    105. </DataGrid>
    106. </Grid>
    107. </Window>
    108.