Coder Social home page Coder Social logo

xamarin-forms-data-binding's Introduction

Xamarin.Forms Data Binding Example

  • DatabindingExample.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DataBinding.DatabindingExample">
    <ContentPage.Content>
        <StackLayout Padding="5,20,5,5">
            <Label Text="Data Binding 101" FontAttributes="Bold" HorizontalOptions="Center"/>
            <Label Text="First Name" />
            <Entry Text="{Binding Forename, Mode=TwoWay}"/>
            <Label Text="Last Name" />
            <Entry Text="{Binding Surename, Mode=TwoWay}"/>
            <StackLayout Orientation="Horizontal">
                <Label Text="{Binding Forename}"/>
                <Label Text="{Binding Surename}"/>
            </StackLayout>
            <StackLayout Padding="0, 20,0,0" IsVisible="{Binding IsJannine}">
                <Image Source="jw.png"/>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
  • DatabindingExample.xaml.cs
using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace DataBinding {
    public partial class DatabindingExample : ContentPage {
        public DatabindingExample () {
            InitializeComponent ();
            BindingContext = new DetailViewModel ();
            Resources = new ResourceDictionary ();
            Resources.Add (new Style (typeof (Image)) {
                Setters = {
                    new Setter { Property = Image.OpacityProperty, Value = .5 }
                }
            });
        }
    }
}
  • DetailViewModel.cs
using System.ComponentModel;

namespace DataBinding {
    public class DetailViewModel: INotifyPropertyChanged {
        private static readonly string JannineName = "Jannine";
        private static readonly string JannineLastName = "Weigel";

        private string _forename = JannineName, _surename = JannineLastName;

        public string Forename {
            get { return _forename; }
            set {
                if (Forename != value) {
                    _forename = value;
                    OnPropertyChanged ("Forename");
                    OnPropertyChanged ("IsJannine");
                }
            }
        }

        public string Surename {
            get { return _surename; }
            set {
                if (Surename != value) {
                    _surename = value;
                    OnPropertyChanged ("Surename");
                    OnPropertyChanged ("IsJannine");
                }
            }
        }

        public bool IsJannine { get {
                return Forename == JannineName && Surename == JannineLastName;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged (string propertyName) {
            var change = PropertyChanged;
            change?.Invoke(this, new PropertyChangedEventArgs (propertyName));
        }
    }
}

Reference

xamarin-forms-data-binding's People

Contributors

wk-j avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.