# .NET and More > WPF, WCF, WF >  WPF : Binding to a class property

## abhijit

How do you bind a property value to a class property?


I have these class properties defined.

C# Code:
private string _hour;
         public string Hour
        {
            get { return _hour; }
            set { _hour = value; }
        }
        private string _minute;
         public string Minute
        {
            get { return _minute; }
            set { _minute = value; }
        }
        private string _second;
         public string Second
        {
            get { return _second; }
            set { _second = value; }
        }

Can someone provide an example of XAML where you can bind these values - Hours, Minutes, Seconds to three separate label controls?

Thanks In Advance.

----------


## DeanMc

In the code behind you set the "DataContext" property of the window to an instance of the class. Then simply do this:



```
<Label x:Name="Hour" {Binding Hour} />
```



```
<Label x:Name="Minute" {Binding Minute} />
```



```
<Label x:Name="Second" {Binding Second} />
```

WPF will look after navigating the controls tree for you  :Wink:

----------


## DeanMc

Just as an aside, you can set the data context at control level too, so say you have a few classes you need to bind to in one window and you have 3 grids containing the 3 sets of controls you can set the datacontext on the control too, like so:



```
myGrid.DataContext = myBindingClass;
```

----------


## DeanMc

How did you get on with this?

----------


## abhijit

> How did you get on with this?


Sorry, I couldn't reply earlier. I will give this a shot sometime on Monday.
I had been away for the weekend.

----------


## abhijit

> In the code behind you set the "DataContext" property of the window to an instance of the class. Then simply do this:
> 
> 
> 
> ```
> <Label x:Name="Hour" {Binding Hour} />
> ```
> 
> 
> ...


I tried this and it throws an error.

<Label {Binding Minute} x:Name="lblMinutes" Height="125"  HorizontalAlignment="Left" Margin="135,12,0,0" VerticalAlignment="Top" Width="100" BorderThickness="2" FontSize="72" >

The error is


Error	2	Character sequence '{' was not expected at this location.	C:\Projects\Nightstand\Nightstand\MainWindow.xaml	14	16	Nightstand

----------


## DeanMc

Sorry my fault, You have to bind to the property in question:



```
<Label x:Name="MyLabel" Text="{Binding Second}"/>
```

----------


## abhijit

I noticed you used a text property. I am using the Content property. Will that make a difference?

----------


## abhijit

Here's my XAML.



```
<Window x:Class="Nightstand.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="228" Width="507">
    <Grid Height="166" Width="403">
            <Label Content="Hours" Name="lblHours" Width="100" BorderThickness="2" Margin="12,12,291,28" FontSize="72" Height="125" Target="{Binding}">
            <Label.Background>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#5C000000" Offset="0.645" />
                    <GradientStop Color="#FFB47676" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
        </Label>
        <Label Height="125"  HorizontalAlignment="Left" Margin="135,12,0,0" VerticalAlignment="Top" Width="100" BorderThickness="2" FontSize="72" x:Name="lblMinutes" DataContext="{Binding ElementName=lblMinutes, Path=Content}">
            <Label.Background>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#5C000000" Offset="0.645" />
                    <GradientStop Color="#FFB47676" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
        </Label>
        <Label  Height="125" HorizontalAlignment="Left" Margin="259,10,0,0" x:Name="lblSeconds" VerticalAlignment="Top" Width="100" BorderThickness="2" FontSize="72" DataContext="{Binding ElementName=lblSeconds, Path=Content}">
            <Label.Background>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#5C000000" Offset="0.645" />
                    <GradientStop Color="#FFB47676" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
        </Label>
    </Grid>
</Window>
```

----------


## DeanMc

Ah I see what your doing wrong:

So in the code beind for the window you wanna do this:



```
PresentationGrid.DataContext = Time(); 'Where time is the name of your time class defined above.
```

Then in the Xaml you want to name your grid PresentationGrid, but it needs to be named like so:



```
<Grid x:Name=PresentationGrid ..../>
```

Now we dont need to know the names of the labels so we can remove all names in them:



```
<Label 
  Content="{Binding Hours}"
  Width="100" 
  BorderThickness="2" 
  Margin="12,12,291,28" 
  FontSize="72" Height="125">
       
  <Label.Background>
       <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
             <GradientStop Color="#5C000000" Offset="0.645" />
             <GradientStop Color="#FFB47676" Offset="1" />
       </LinearGradientBrush>
  </Label.Background>
</Label>
```

----------


## abhijit

Will I not need to create an instance of the class Time?

----------


## DeanMc

Yes in the code behind. Before you set it to the data context!

----------


## abhijit

I used the following code.

C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
 namespace Nightstand
{
    public class MyTime
    {
        private string _second;
         public string Second
        {
            get { return _second; }
            set { _second = value; }
        }
         private string _hour;
         public string Hour
        {
            get { return _hour; }
            set { _hour = value; }
        }
        private string _minute;
         public string Minute
        {
            get { return _minute; }
            set { _minute = value; }
        }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MyTime my;
        private Timer tmr;
         private bool _show;
         public bool showMe
        {
            get { return _show; }
            set { _show = value; }
        }
        public MainWindow()
        {
            InitializeComponent();
            my= new MyTime();
            PresentationGrid.DataContext = my;
            tmr = new Timer();       // Doesn't require any args
            tmr.Interval = 5;
            showMe = false;
            tmr.Elapsed += refreshTime;    // Uses an event instead of a delegate
            // Start the timer
            tmr.Start();
            
        }
        private void refreshTime(object sender, EventArgs e)
        {
            my.Minute = DateTime.Now.Minute.ToString();
            my.Hour = DateTime.Now.Hour.ToString();
            my.Second = DateTime.Now.Millisecond.ToString();
        }
    }
}

This is my XAML.


```
<Window x:Class="Nightstand.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="228" Width="507">
    <Grid x:Name="PresentationGrid" Height="166" Width="403">
            <Label Content="{Binding Hour}" Width="100" BorderThickness="2" Margin="12,12,291,28" FontSize="72" Height="125" Target="{Binding}">
            <Label.Background>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#5C000000" Offset="0.645" />
                    <GradientStop Color="#FFB47676" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
        </Label>
        <Label Height="125"  Content ="{Binding Minute}" HorizontalAlignment="Left" Margin="135,12,0,0" VerticalAlignment="Top" Width="100" BorderThickness="2" FontSize="72">
            <Label.Background>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#5C000000" Offset="0.645" />
                    <GradientStop Color="#FFB47676" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
        </Label>
        <Label  Height="125" Content ="{Binding Second}" HorizontalAlignment="Left" Margin="259,10,0,0" VerticalAlignment="Top" Width="100" BorderThickness="2" FontSize="72">
            <Label.Background>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#5C000000" Offset="0.645" />
                    <GradientStop Color="#FFB47676" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
        </Label>
    </Grid>
</Window>
```

I am not able to refresh the hours, minutes, seconds in my label controls.
What I am doing wrong?

----------


## DeanMc

Yeah I thought you would eventually get to this issue, rather than me explaining it to you have a look at this post with special attention to INotifyPropertChanged. It should explain everything you need to know!

----------

