Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • DataContext in wpf

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 154
    Comment on it

    DataContext is one of the basic concepts in Data Binding which make sure to inherits the information from their parent element that is used for Binding and the other characteristics of the binding. It is extremely useful while designing WPF applications. In Data Binding, gets or sets the data context for an element. DataContext provides data to Binding by using Source property directly. DataContext defined on the FrameworkElement class. The DataContext property is the default source of your bindings, unless you specifically declare another source. The default value is null of source of for the DataContext property. If you set the DataContext for the Window itself and then use it throughout all of the child controls.

    Look at following example:

    Album.cs

    namespace MySampleWpfApplication
    {
        public class Album
        {
            private string artist;
            private string duration;
            private string genre;
            private string name;
    
            public Album()
            {
                artist = duration = genre = name = null;
            }
    
            public string Artist
            {
                get { return artist; }
                set { artist = value; }
            }
    
            public string Duration
            {
                get { return duration; }
                set { duration = value; }
            }
    
            public string Genre
            {
                get { return genre; }
                set { genre = value; }
            }
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
        }
    }


    DataContextSample.xaml

    <Window x:Class="MySampleWpfApplication.DataContextSample"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:MySampleWpfApplication"
            mc:Ignorable="d"
            Title="Album Details" Height="150" Width="300">
        <Window.Resources>
            <local:Album x:Key="MyAlbum" Artist="Various artists" Duration="5" Genre="First generation" Name="My first album"></local:Album>
        </Window.Resources>
        <Grid DataContext="{StaticResource MyAlbum}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions >
                <ColumnDefinition Width="130px" />
                <ColumnDefinition Width="178*" />
            </Grid.ColumnDefinitions>
    
            <Label Grid.Row="0" Grid.Column="0">Album Name:</Label>
            <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=Name}"></TextBox>
    
            <Label Grid.Row="1" Grid.Column="0">Generation:</Label>
            <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=Genre}"></TextBox>
    
            <Label Grid.Row="2" Grid.Column="0">Artist:</Label>
            <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=Artist}" />
    
            <Label Grid.Row="3" Grid.Column="0">Duration:</Label>
            <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Path=Duration}"></TextBox>
        </Grid>
    </Window>

    In the above example, I have created a class Album.cs which has Artist, Duration, Genre and Name properties. I am setting  the DataContext on an element to a Album object. It means that  all data bindings contained within that root element’s element tree will automatically bind against the Album object, unless explicitly bind against something else.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: