4
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

XamarinのBindingについて

XamarinのBindingでご質問

エクセルソフトの田淵さんがこんなことをつぶやいておられました。

まあ、こう考えたのですが。

実はこうである、と。

やってみた

まあ、確かにBinding難しいよなー。
とかつらつら考えながらコーディング。
できました。

BindingContextがミソ

    public class App : Xamarin.Forms.Application
    {
        Switch sw1;
        Switch sw2;
        Switch sw3;
        Entry ent;

        public App() {
            sw1 = new Switch () {
            };
            sw2 = new Switch () {
            };
            sw2.BindingContext = sw1;
            sw2.SetBinding (Switch.IsToggledProperty, new Binding ("IsToggled", BindingMode.OneWay));
            sw3 = new Switch () {
            };
            sw3.BindingContext = sw1;
            sw3.SetBinding (Switch.IsToggledProperty, new Binding ("IsToggled", BindingMode.OneWay));
            ent = new Entry () {
                Text = "Hello, world",
            };
            ent.BindingContext = sw1;
            ent.SetBinding (Entry.IsEnabledProperty, new Binding ("IsToggled", BindingMode.OneWay));
            MainPage = new ContentPage () {
                Content = new StackLayout() {
                    Padding = new Thickness(0, 20, 0,0),
                    Orientation = StackOrientation.Vertical,
                    Children = {
                        new StackLayout() {
                            Orientation = StackOrientation.Horizontal,
                            Children = {
                                sw1,
                                new Label() {
                                    Text = "<- switch this",
                                },
                            },
                        },
                        sw2,
                        sw3,
                        ent,
                    },
                },
            };
        }
    }

まあ要するに、先スイッチのBindingContextを元スイッチのオブジェクトにして仕舞えばいいわけですね。
XAMLだと…。
どうするかなぁ。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
4
Help us understand the problem. What are the problem?