DataBindingの使い方

Form1.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PropertyChangedTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            //・フォームにBindingSourceコントロールを追加
            //・Options.csを追加(InotifyPropertyChangedを継承)
            //・public event PropertyChangedEventHandler PropertyChanged;←これに値を入れてあげないと
            //  画面に反映されない(INotifyPropertyChangedの動的反映用?)
            //・PropertyChanged += OnPropertyChangedで値の変更時に指定メソッドを読んでもらうようにする
            //・画面に反映させたいプロパティをpublicで宣言
            //・プロパティに指定したい値(初期値とか)を格納する
            //・ビルド
            //・デザイナから、bindingsourceで指定したいプロパティを指定する
            //  コントロール⇒プロパティ⇒databindingsource⇒詳細⇒Font⇒optiondatabindingsource.ContentFont、OnpropertyChanged
            //・因みにoptionbindingsorceもPropertyChangedTest.Optionsを指定していること          
            
            InitializeComponent();
            this.optionsBindingSource.DataSource = Options.Instance;            
        }
    }
}



Option.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PropertyChangedTest
{
    /// <summary>
    /// 画面の状態を設定
    /// </summary>
    public class Options : INotifyPropertyChanged
    {
        /// <summary>
        /// プロパティ変更時イベント
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        public Font ContentFont { get; private set; }

        private static Options instance = new Options();

        /// <summary>
        /// 実装の隠蔽
        /// </summary>
        private Options()
        {

            PropertyChanged += OnPropertyChanged;
            Zooming(false);
        }

        /// <summary>
        /// シングルトンインスタンス
        /// </summary>
        public static Options Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// プロパティが変更された場合に値を変更する
        /// </summary>
        /// <param name="aSender"></param>
        /// <param name="aEvent"></param>
        private void OnPropertyChanged(object aSender, EventArgs aEvent)
        {
            //TODO 状況によりtrueにするかfalseにするか判定する処理が必要
            Zooming(true);
        }


        private void Zooming( bool aZoom )
        {
            if (!aZoom)
            {
                this.ContentFont = Properties.Settings.Default.ContentFont;
            }
            else
            {
                this.ContentFont = Properties.Settings.Default.ContentFont;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ContentFont"));
                }
            }
        }
    }
}