Visual Studio / WPF > object > 03 sender是什么 > senderに基づく処理


動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

03 sender是什么
https://www.youtube.com/watch?v=VnaZpOif8DU&t=182s&list=PLaYqF7AnyNPfTfZiGqTwedIKW_FkZv4Xj&index=3

button2_Click()のsenderを使った例。
2:00ころから。

試してみた。

MainWindow.xaml
<Window x:Class="_170508_t0715_sender.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:_170508_t0715_sender"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Button1" Click="Button_Click"/>

    </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 _170508_t0715_sender
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            btn.Content = "Clicked";
        }
    }
}

ボタンクリック前。

ボタンクリック後。

補足事項

「senderが何か」に基づいて処理を分岐するというのはC++ Builderでもよく使った。

senderに基づく共通処理を実装して、複数のコントロールでのClickイベントにその共通処理を割り当てる、というような使い方。

中国語

sender是什么
http://cjjc.weblio.jp/content/%E4%BB%80%E4%B9%88

是什么 (shi・shén・me)
意訳「sender。これ何ですか?」