Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How many ways we can use datacontext in silverlight 4 .


when we use DataContext In XMAL and when we use DataContext in code behand like (Main.xmal.cs)
Posted

1 solution

As the best practice, you can set Data Context in XAML and get it in code behind on the various points.

Here is the sample code. You can instantiate your class in XAML (within the UserControl.Resources) and then bind the datacontext to a static resource.

XML
<UserControl ...>
    <UserControl.Resources>
       <myNS:MyClass x:Name="TheContext" x:Key="TheContext"></myNS:MyClass>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource TheContext}" >
        <TextBlock Text="{Binding Path=Field1}">
        </TextBlock>
    </Grid>
</UserControl>
 
Share this answer
 
Comments
ramesh.mandapelly 20-Jun-12 23:52pm    
Hi , thanks in advance for helping me . but i am expectind something more elabarative , please if possible help me on that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900