Data Binding in Windows Phone Application

Here is an example which will briefly explain the Data Binding in windows phone application.
The XAML code for the main page is as follows:

<Grid>
        <StackPanel Margin="30 ,100">
            <TextBox Name="txtbox"/>
            <Button Name="btnchng" Content="Change Data" Click="btnchng_Click"/>
            <ListBox Name="listbox1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*"></ColumnDefinition>
                                <ColumnDefinition Width="3*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Image Grid.Column="0" Source="{Binding ImagePath}" Width="100"/>
                            <StackPanel Grid.Column="1" Orientation="Vertical">
                                <TextBox Text="{Binding FullName, Mode=TwoWay}" FontSize="40" FontWeight="Bold"></TextBox>
                                <TextBox Text="{Binding Email, Mode=TwoWay}" FontSize="30"></TextBox>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Grid>

The designer view for this is shown as under:


Now it’s time to add a class to your project. For which I have created a person class and the code for which is as follows:
namespace DataBinding1
{
        public class Person : INotifyPropertyChanged
        {

            private string _img, _fname, _email;
            public string ImagePath
            {
                get
                {
                    return _img;
                }
                set
                {
                    _img = value;
                }
            }
            public string FullName
            {
                get
                {
                    return _fname;

                }
                set
                {
                    _fname = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("FullName"));
                }
            }
            public string Email
            {
                get
                {
                    return _email;
                }
                set
                {
                    _email = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("Email"));
                }

            }

            public event PropertyChangedEventHandler PropertyChanged;
        }
}

Now come to the mainPage.xaml.cs file and write this code:

namespace DataBinding1
{
    public partial class MainPage : PhoneApplicationPage
    {
        List<Person> _list;
        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            _list = new List<Person>();
            _list.Add(new Person { FullName = "afsar murad", Email = "afsar.murad@gmail.com ", ImagePath = "/Images/1.jpg" });
            _list.Add(new Person { FullName = "Hamid ali", Email = "hamid@gmail.com ", ImagePath = "/Images/2.jpg" });
            _list.Add(new Person { FullName = "Essa ali", Email = "essa@gmail.com ", ImagePath = "/Images/3.jpg" });
            _list.Add(new Person { FullName = "inayat ali", Email = "inayat@gmail.com ", ImagePath = "/Images/4.png" });


            listbox1.ItemsSource = _list;
        }

        private void btnchng_Click(object sender, RoutedEventArgs e)
        {

            _list[0].FullName = txtbox.Text;
            //_list[0].Email = txtbox.Text;
            // await new MessageDialog(_list[0].FullName).ShowAsync(); 
        }
      
    }
}

Data Binding in Windows Phone Application Data Binding in Windows Phone Application Reviewed by Afsar Murad on 02:16 Rating: 5
Powered by Blogger.