やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

DataBindingで連動してみた

DataBindingにてNumericUpDownとTrackBarを連動させてみた。

f:id:ytyaru:20160619160145p:plain

入手先

GitHub MEGA

DataBinding

前回はイベントにて連動させていました。 今回は、DataBindingという概念にて連動させてみました。

どちらかのUIを操作すると別のUIのほうも変化します。 また、プログラムでデータを設定することで両方のUIを一度に設定できます。

  • System.Windows.Form.DataSource
  • System.ComponentModel.INotifyPropertyChanged
  • System.ComponentModel.PropertyChangedEventHandler

データクラス

public class RGB : INotifyPropertyChanged
    {
        private int _R;
        private int _G;
        private int _B;

        public int R { get { return _R; } set { _R = value; NotifyPropertyChanged("R"); } }
        public int G { get { return _G; } set { _G = value; NotifyPropertyChanged("G"); } }
        public int B { get { return _B; } set { _B = value; NotifyPropertyChanged("B"); } }

        public event PropertyChangedEventHandler PropertyChanged = (_, __) => { };
        private void NotifyPropertyChanged(string propertyName = "")
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

フォーム

public partial class Form1 : Form
    {
        private BindingSource bindingSource1 = new BindingSource();

        public Form1()
        {
            InitializeComponent();

            this.numericUpDown1.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource1, "R", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
            this.numericUpDown2.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource1, "G", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
            this.numericUpDown3.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource1, "B", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

            this.trackBar1.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource1, "R", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
            this.trackBar2.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource1, "G", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
            this.trackBar3.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource1, "B", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

            RGB rgb1 = new RGB();
            rgb1.R = 100;
            rgb1.G = 200;
            rgb1.B = 50;
            this.bindingSource1.DataSource = rgb1;

            ((RGB)(this.bindingSource1.DataSource)).R = 5;
            ((RGB)(this.bindingSource1.DataSource)).G = 205;
            ((RGB)(this.bindingSource1.DataSource)).B = 55;
        }
    }

GUIデザイナーが壊れる

最初はVisual C# 2010 Express のツールバーにて DataSource を追加する方法でやっていました。 しかし、以下のようにデザイナが機能しなくなってしまいました。 なので、デザイナは持ちいらず、コードで記述することにしました。

f:id:ytyaru:20160619161207p:plain

プロパティエクスプローラでDataSourceの詳細をクリックするとエラー表示される。

f:id:ytyaru:20160619161214p:plain

f:id:ytyaru:20160619161226p:plain f:id:ytyaru:20160619161232p:plain