LoginSignup
0
0

More than 5 years have passed since last update.

Visual Studio / WPF > 14 布局案例:编写连连看布局 > Window_Loaded()にてStackPanelにButtonを追加する例

Last updated at Posted at 2017-05-11
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

14 布局案例:编写连连看布局
https://www.youtube.com/watch?v=PkWk1vkAAmA&list=WL&index=31

2:00あたりでStackPanelに対して、Window_Loaded()イベントにてButtonを追加する例が紹介されている。

試してみた。

code

MainWindow.xaml
<Window x:Class="_170512_t0716_buttonAdd.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170512_t0716_buttonAdd"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel x:Name="sp1" Grid.Column="2" Background="blue"></StackPanel>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _170512_t0716_buttonAdd
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Button btn = new Button();
            btn.Content = "hello";

            sp1.Children.Add(btn);
        }
    }
}

デザイン / 実行例

デザイナ上
work.png

実行直後
work.png

中国語 > 连连

14 布局案例:编写连连看布局の「连连」

liánlián
(多く複音節動詞を修飾し;言葉・動作が途切れないで)続けざまに,何度も続けて.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0