上code:
- <Window x:Class="WpfApplication1.MainWindow"
- xmlns=""
- xmlns:x=""
- Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
- <Grid>
- <Canvas Name="abc" Margin="0,57,0,0">
- <Button Canvas.Left="400" Canvas.Top="207" Content="Button" Height="30" Name="Button2" Width="85" Click="btn_click" />
- </Canvas>
- </Grid>
- </Window>
上面是布局的时候加了个按钮,下面的代码中会加入个程序中创建的。
- namespace WpfApplication1
- {
- ///
- /// MainWindow.xaml 的交互逻辑
- ///
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
-
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- Button btn = new Button
- {
- Name = "Button1",
- Content = "OK",
- Height = 23,
- Width = 64,
- HorizontalAlignment = HorizontalAlignment.Left,
- Margin = new Thickness(10, 10, 0, 0),
- VerticalAlignment = VerticalAlignment.Top,
- Visibility = Visibility.Visible
- };
- btn.Click += new RoutedEventHandler(btn_click);
- abc.Children.Add(btn);
- }
- private void btn_click(object sender, RoutedEventArgs e)
- {
- Button btn = sender as Button;
- if ("Button1" == btn.Name)
- {
- System.Windows.MessageBox.Show("hello");
- }
- else if ("Button2" == btn.Name)
- {
- System.Windows.MessageBox.Show("World.");
- }
- }
- }
- }
阅读(14146) | 评论(0) | 转发(0) |