事件处理器与代码后置

事件模型:
image.png
事件拥有者:btn
事件:click
响应者:MainWindow窗体
处理器:btn1_Click()方法
事件订阅:Click = “btn1_Click”

C#代码实现订阅 this.button.click+= xxx;

程序集-组件化程序设计

组件:WPF UserControl

  1. <UserControl x:Class="UserControlLibrary.UserControl1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:UserControlLibrary"
  7. mc:Ignorable="d"
  8. d:DesignHeight="300" d:DesignWidth="300" Background="LightBlue">
  9. <Canvas>
  10. <Label Content="基本工资" Canvas.Left="45" Canvas.Top="55"/>
  11. <Label Content="岗位工资" Canvas.Left="45" Canvas.Top="113"/>
  12. <Label Content="实际工资" Canvas.Left="45" Canvas.Top="171"/>
  13. <Button Content="Calc" Canvas.Left="54" Canvas.Top="219" RenderTransformOrigin="0.056,0.576" Width="192" Click="btn_Click"/>
  14. <TextBox x:Name="textBox1" Canvas.Left="126" Canvas.Top="59" Text="TextBox" TextWrapping="Wrap" Width="120"/>
  15. <TextBox x:Name="textBox2" Canvas.Left="126" Canvas.Top="117" Text="TextBox" TextWrapping="Wrap" Width="120"/>
  16. <TextBox x:Name="textBox3" Canvas.Left="126" Canvas.Top="175" Text="TextBox" TextWrapping="Wrap" Width="120"/>
  17. </Canvas>
  18. </UserControl>

MainWindow:
1)添加引用-将组件的引用添加进入
2)xmlns:uercontrol = “”
3) 组件添加进去

  1. <Window x:Class="GridAddUserControl.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:usercontrol="clr-namespace:UserControlLibrary;assembly=UserControlLibrary"
  6. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  7. xmlns:local="clr-namespace:GridAddUserControl"
  8. mc:Ignorable="d"
  9. Title="MainWindow" Height="450" Width="800">
  10. <Grid>
  11. <Grid.ColumnDefinitions>
  12. <ColumnDefinition/>
  13. <ColumnDefinition/>
  14. </Grid.ColumnDefinitions>
  15. <Grid.RowDefinitions>
  16. <RowDefinition/>
  17. <RowDefinition/>
  18. </Grid.RowDefinitions>
  19. <usercontrol:UserControl1 Grid.Column="0" Grid.Row="0"/>
  20. <usercontrol:UserControl1 Grid.Column="0" Grid.Row="1"/>
  21. <usercontrol:UserControl1 Grid.Column="1" Grid.Row="0"/>
  22. <usercontrol:UserControl1 Grid.Column="1" Grid.Row="1"/>
  23. </Grid>
  24. </Window>

注释

添加注释Ctrl+K Ctrl+C
取消注释Ctrl+K Ctrl+U