Esempio n. 1
0
        public void Bind(NotifyObject notify, string name, NumericUpDown c, DataBindWay way = DataBindWay.TwoWay)
        {
            // 値が変更になった時にデータバインドしているほうに値を戻す。
            var control = c; // copy for capture

            var h1 = new PropertyChangedEventHandler((args) =>
            {
                // 値が範囲外なら補整してからセットする。
                var v = (int)args.value;
                if (c.Maximum < v)
                {
                    v = (int)c.Maximum;
                }
                if (c.Minimum > v)
                {
                    v = (int)c.Minimum;
                }
                control.Value = v;
            });

            notify.AddPropertyChangedHandler(name, h1);

            EventHandler h2 = null;

            if (way == DataBindWay.TwoWay)
            {
                h2              = new EventHandler((sender, args) => { notify.SetValue <int>(name, (int)control.Value); });
                c.ValueChanged += h2;
            }

            AddHandler <int>(notify, name, h1, control, h2);
        }
Esempio n. 2
0
        /// <summary>
        /// DataBindする。別のNotifyObjectの同名のプロパティと紐づけられる。
        /// way : OneWay  片方向のbinding(thisのnameが変更されたときにnotify.nameに値がコピーされる。)
        /// way : TwoWay  双方向のbinding
        /// </summary>
        /// <param name="name"></param>
        /// <param name="notify"></param>
        /// <param name="way"></param>
        public void Bind(string name, NotifyObject notify, DataBindWay way)
        {
            lock (lockObject)
            {
                bind_helper(name, notify);

                // 双方向なので逆からもbindしてやる。
                if (way == DataBindWay.TwoWay)
                {
                    notify.bind_helper(name, this);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// ComboBoxとbindする。
        /// propertyの型はint。
        /// name : NotifyObjectで実装されたプロパティ名
        /// </summary>
        /// <param name=""></param>
        /// <param name="c"></param>
        /// <param name="setter"></param>
        public void Bind(NotifyObject notify, string name, ComboBox c, Action <int> f = null, DataBindWay way = DataBindWay.TwoWay)
        {
            // 値が変更になった時にデータバインドしているほうに値を戻す。
            var control = c; // copy for capture

            var h1 = new PropertyChangedEventHandler((args) =>
            {
                // 値が範囲外なら補整してからセットする。
                // ComboBoxのitemがないこともあるので注意。
                var v = (int)args.value;
                if (control.Items.Count != 0)
                {
                    if (control.Items.Count <= v)
                    {
                        v = 0;
                    }
                    if (v < 0)
                    {
                        v = 0;
                    }
                    control.SelectedIndex = v;
                }

                // おまけハンドラがあるなら呼び出す。
                if (f != null)
                {
                    f(v);
                }
            });

            notify.AddPropertyChangedHandler(name, h1);

            EventHandler h2 = null;

            if (way == DataBindWay.TwoWay)
            {
                h2 = new EventHandler((sender, args) => {
                    //Console.WriteLine($"{name} = {(int)control.SelectedIndex}");
                    notify.SetValue <int>(name, (int)control.SelectedIndex);
                });
                c.SelectedIndexChanged += h2;
            }

            AddHandler <int>(notify, name, h1, control, h2);
        }
Esempio n. 4
0
        /// <summary>
        /// dicで日本語化する。
        /// dic.Add("no_book","定跡なし");のようになっている。
        /// </summary>
        /// <param name="notify"></param>
        /// <param name="name"></param>
        /// <param name="c"></param>
        /// <param name="f"></param>
        /// <param name="dic"></param>
        /// <param name="way"></param>
        public void BindStringWithDic(NotifyObject notify, string name, ComboBox c, Action f = null, Dictionary <string, string> dic = null, DataBindWay way = DataBindWay.TwoWay)
        {
            // 値が変更になった時にデータバインドしているほうに値を戻す。
            var control = c; // copy for capture

            var h1 = new PropertyChangedEventHandler((args) =>
            {
                var vs = (string)args.value; // この項目を探して、それを選択する。
                if (!dic.ContainsKey(vs))
                {
                    return;
                }
                vs = dic[vs]; // 辞書で変換

                if (control.Items.Count != 0)
                {
                    for (int i = 0; i < control.Items.Count; ++i)
                    {
                        if (vs == control.Items[i].ToString())
                        {
                            control.SelectedIndex = i;
                            break;
                        }
                    }
                }

                // おまけハンドラがあるなら呼び出す。
                if (f != null)
                {
                    f();
                }
            });

            notify.AddPropertyChangedHandler(name, h1);

            EventHandler h2 = null;

            if (way == DataBindWay.TwoWay)
            {
                h2 = new EventHandler((sender, args) => {
                    var selected = control.SelectedItem.ToString();
                    // dicを使って逆変換してから、SetValueする。
                    foreach (var k in dic)
                    {
                        if (k.Value == selected)
                        {
                            notify.SetValue <string>(name, (string)k.Key);
                            break;
                        }
                    }
                });
                c.SelectedIndexChanged += h2;
            }

            AddHandler <string>(notify, name, h1, control, h2);
        }
Esempio n. 5
0
        public void BindString(NotifyObject notify, string name, ComboBox c, Action f = null, DataBindWay way = DataBindWay.TwoWay)
        {
            // 値が変更になった時にデータバインドしているほうに値を戻す。
            var control = c; // copy for capture

            var h1 = new PropertyChangedEventHandler((args) =>
            {
                var vs = (string)args.value; // この項目を探して、それを選択する。

                if (control.Items.Count != 0)
                {
                    for (int i = 0; i < control.Items.Count; ++i)
                    {
                        if (vs == control.Items[i].ToString())
                        {
                            control.SelectedIndex = i;
                            break;
                        }
                    }
                }

                // おまけハンドラがあるなら呼び出す。
                if (f != null)
                {
                    f();
                }
            });

            notify.AddPropertyChangedHandler(name, h1);

            EventHandler h2 = null;

            if (way == DataBindWay.TwoWay)
            {
                h2 = new EventHandler((sender, args) => {
                    notify.SetValue <string>(name, (string)control.SelectedItem);
                });
                c.SelectedIndexChanged += h2;
            }

            AddHandler <string>(notify, name, h1, control, h2);
        }
Esempio n. 6
0
        public void BindString(NotifyObject notify, string name, CheckBox c, Action f = null, DataBindWay way = DataBindWay.TwoWay)
        {
            // 値が変更になった時にデータバインドしているほうに値を戻す。
            var control = c; // copy for capture

            var h1 = new PropertyChangedEventHandler((args) =>
            {
                var vs = (string)args.value;
                var v  = vs == "true" ? true : false; // USI上、小文字しか許容していない。

                // 値が範囲外なら補整してからセットする。
                control.Checked = v;
                if (f != null)
                {
                    f();
                }
            });

            notify.AddPropertyChangedHandler(name, h1);

            EventHandler h2 = null;

            if (way == DataBindWay.TwoWay)
            {
                h2 = new EventHandler((sender, args) => { notify.SetValue <string>(name, control.Checked ? "true" : "false"); });
                c.CheckedChanged += h2;
            }

            AddHandler <string>(notify, name, h1, control, h2);
        }
Esempio n. 7
0
 public void BindString(NotifyObject notify, string name, TextBox c, Action f = null, DataBindWay way = DataBindWay.TwoWay)
 {
     // これはそのまま移譲しとけば良い。
     Bind(notify, name, c, (string s) => { f(); }, way);
 }
Esempio n. 8
0
        /// <summary>
        /// nofityのnameがstring型の時にbindする。
        /// </summary>
        /// <param name="notify"></param>
        /// <param name="name"></param>
        /// <param name="c"></param>
        /// <param name="way"></param>
        public void BindString(NotifyObject notify, string name, NumericUpDown c, Action f = null, DataBindWay way = DataBindWay.TwoWay)
        {
            // 値が変更になった時にデータバインドしているほうに値を戻す。
            var control = c; // copy for capture

            var h1 = new PropertyChangedEventHandler((args) =>
            {
                var vs = (string)args.value;
                int v;
                if (!int.TryParse(vs, out v))
                {
                    return;
                }

                // 値が範囲外なら補整してからセットする。
                if (c.Maximum < v)
                {
                    v = (int)c.Maximum;
                }
                if (c.Minimum > v)
                {
                    v = (int)c.Minimum;
                }
                control.Value = v;

                f();
            });

            notify.AddPropertyChangedHandler(name, h1);

            EventHandler h2 = null;

            if (way == DataBindWay.TwoWay)
            {
                h2              = new EventHandler((sender, args) => { notify.SetValue <string>(name, control.Value.ToString()); });
                c.ValueChanged += h2;
            }

            AddHandler <string>(notify, name, h1, control, h2);
        }
Esempio n. 9
0
        public void Bind(NotifyObject notify, string name, RadioButton c, Action <bool> f = null, DataBindWay way = DataBindWay.TwoWay)
        {
            // 値が変更になった時にデータバインドしているほうに値を戻す。
            var control = c; // copy for capture

            var h1 = new PropertyChangedEventHandler((args) =>
            {
                // 値が範囲外なら補整してからセットする。
                var v           = (bool)args.value;
                control.Checked = v;
                if (f != null)
                {
                    f(v);
                }
            });

            notify.AddPropertyChangedHandler(name, h1);

            EventHandler h2 = null;

            if (way == DataBindWay.TwoWay)
            {
                h2 = new EventHandler((sender, args) => { notify.SetValue <bool>(name, control.Checked); });
                c.CheckedChanged += h2;
            }

            AddHandler <bool>(notify, name, h1, control, h2);
        }
Esempio n. 10
0
        /// <summary>
        /// convを通してboolをstringに置き換えてからButtonに設定する。
        /// </summary>
        public void Bind(NotifyObject notify, string name, Button c, Func <bool, string> conv, DataBindWay way = DataBindWay.TwoWay)
        {
            // 値が変更になった時にデータバインドしているほうに値を戻す。
            var control = c; // copy for capture

            var h1 = new PropertyChangedEventHandler((args) =>
            {
                // 値が範囲外なら補整してからセットする。
                var v        = conv((bool)args.value);
                control.Text = v;
            });

            notify.AddPropertyChangedHandler(name, h1);

            EventHandler h2 = null;

            if (way == DataBindWay.TwoWay)
            {
                h2             = new EventHandler((sender, args) => { notify.SetValue <bool>(name, control.Text == conv(true)); });
                c.TextChanged += h2;
            }

            AddHandler <bool>(notify, name, h1, control, h2);
        }