Showing posts with label Data Binding. Show all posts
Showing posts with label Data Binding. Show all posts

Saturday, January 25, 2014

In this example, we use data binding and bind a slider with a text. When we play with slider, the text will change.

App Name: DataBSlider


Actually, this task can be accomplished without using C#. Once we create a slide and textbox, we can bind them in XAML.  Add the following lines in your XAML file to achieve this.

<Slider x:Name="mySlider1" HorizontalAlignment="Left" Margin="54,223,0,0" VerticalAlignment="Top" Width="318" Value="50" Minimum="0" Maximum="100" />
            <TextBlock HorizontalAlignment="Left" Margin="143,125,0,0" TextWrapping="Wrap" Text="{Binding Value, ElementName=mySlider1, StringFormat=F1}" VerticalAlignment="Top" Height="28" Width="173"/>

Now, you should have an app with a textbox whose value is controlled with a slide bar as shown in figure 1

Figure 1
However, we want to bind the slider to an integer since we can do a lot more this way.   We are going to create two properties: sliderValue and sliderText. sliderText will be the property related to text box and sliderValue will be associated with the value coming from the slider. To do that, in XAML we use the following:

<Slider x:Name="mySlider1" HorizontalAlignment="Left" Margin="54,223,0,0" VerticalAlignment="Top" Width="318" Value="{Binding sliderValue, Mode=TwoWay}" Minimum="0" Maximum="100" />
            <TextBlock HorizontalAlignment="Left" Margin="143,125,0,0" TextWrapping="Wrap" Text="{Binding sliderText, Mode=TwoWay}" VerticalAlignment="Top" Height="28" Width="173"/>

And here’s how my mainpage.xaml.cs looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using DataBSlider.Resources;
using System.ComponentModel;

namespace DataBSlider
{
    public class DataControls : INotifyPropertyChanged
    {

        public string sliderText
        {
            get { return Convert.ToString(sliderValue); }
        
        }

        public int _sliderValue;
        public int sliderValue
        {
            get { return _sliderValue; }
            set { _sliderValue=value; Notify("sliderText"); Notify("sliderValue"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));

        }
    }
   
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        DataControls myData;
        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;


        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            myData = new DataControls();
            myData.sliderValue = 20;
            ContentPanel.DataContext = myData;
        }


    }
}


The code works! I get a similar output as before but since I used an integer, the value is read as an integer. Image 2 shows my output for the code


Figure 2

Posted on 7:14 PM by Erdem

No comments

In this example, we are moving one step further with data binding to a control element and we are updating our binding element with implementing INotifyPropertyChanged

The first task is to figure out how to group radio buttons. For example, you might have 2 group of options to select for a patient recording program. You might want to record the gender and age. If you want to do it with radio buttons, you’d have something like in the image



Here, gender and age range options are grouped differently so we can have two selected options at once. (If they were all grouped into one group, you’d only be able to select one option). This can be done in XAML, using the following:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <RadioButton GroupName="ButtonGroup1" Content="12 or younger" HorizontalAlignment="Left" Margin="36,61,0,0" VerticalAlignment="Top"/>
            <RadioButton GroupName="ButtonGroup1" Content="Between 12 and 18" HorizontalAlignment="Left" Margin="36,136,0,0" VerticalAlignment="Top"/>
            <RadioButton GroupName="ButtonGroup1" Content="18 or older" HorizontalAlignment="Left" Margin="36,211,0,0" VerticalAlignment="Top"/>

            <RadioButton GroupName="ButtonGroup2" Content="Female" HorizontalAlignment="Left" Margin="29,338,0,0" VerticalAlignment="Top"/>
            <RadioButton GroupName="ButtonGroup2" Content="Male" HorizontalAlignment="Left" Margin="29,388,0,0" VerticalAlignment="Top"/>
            <TextBlock HorizontalAlignment="Left" Margin="50,29,0,0" TextWrapping="Wrap" Text="Select Age Range" VerticalAlignment="Top" Height="32" Width="155"/>
            <TextBlock HorizontalAlignment="Left" Margin="50,306,0,0" TextWrapping="Wrap" Text="Select Gender" VerticalAlignment="Top" Height="32" Width="155"/>

        </Grid>



Now that we figured how to add multiple groups of radio buttons, let's go back to data binding. Now, I want to add another text that is controlled by the radio buttons. This text should have the information related to patient’s gender. In addition, it should update every time I select the corresponding radio button. First, I add this text in XAML as:


<TextBlock x:Name="GenderText" HorizontalAlignment="Left" Margin="36,491,0,0" TextWrapping="Wrap" Text="{Binding genderText, Mode=TwoWay}" VerticalAlignment="Top" Height="32" Width="352"/>

 In order to update this text with changes in radio buttons, the program should be able to tell if the status of the radio buttons are updated as well. For this, I need “Two Way Binding” so I have to change my radio buttons code in XAML file as:

            <RadioButton GroupName="ButtonGroup2" Content="Female" HorizontalAlignment="Left" Margin="29,338,0,0" VerticalAlignment="Top" IsChecked="{Binding patientFemale, Mode=TwoWay}"/>
            <RadioButton GroupName="ButtonGroup2" Content="Male" HorizontalAlignment="Left" Margin="29,388,0,0" VerticalAlignment="Top" IsChecked="{Binding patientMale, Mode=TwoWay}"/>


Now, my work with XAML is done and I need to update my MaingPage.xaml.cs In short, here’s a list of what I need to do in my .cs file to implement the code:
1. I need to inherit from INotifyPropertyChanged so when a property changes, I can detect it and do as I wish
2. I need to add string and Boolean properties to control the flow of the program and change the variables as I wish
Here’s how my .cs file looks like:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using RadioButtonsBinding.Resources;


namespace RadioButtonsBinding
{
    public class Patient : INotifyPropertyChanged
    {
        public bool _patientMale;
        public bool patientMale
        {
            get {return _patientMale;}
            set { _patientMale = value; Notify("patientMale"); Notify("genderText"); }
        }

        public bool _patientFemale;
        public bool patientFemale
        {
            get { return _patientFemale; }
            set { _patientFemale = value; Notify("patientFemale"); Notify("genderText"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));

        }

        public string _genderText;
        public string genderText
        {
            get
            {
                string common_string = "The gender of the patient is: ";
                if (_patientFemale)
                { return common_string+"Female"; }
                else { return common_string + "Male"; }
            }

        }
       
    }

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        Patient newPatient;

        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            newPatient = new Patient();
            ContentPanel.DataContext = newPatient;
        }



    }
}


Now the program works as I want and here are the screen outputs:





The text dynamically changes every time I select a different gender with the radio button!


Posted on 3:41 PM by Erdem

No comments