LoginSignup
6
4

More than 5 years have passed since last update.

XamarinのBindingについて

Posted at

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だと…。
どうするかなぁ。

6
4
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4