我们可以在Silverlight 4中使用数据上下文的几种方法 [英] How many ways we can use datacontext in silverlight 4

查看:52
本文介绍了我们可以在Silverlight 4中使用数据上下文的几种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在Silverlight 4中使用数据上下文的几种方法.


当我们在XMAL中使用DataContext以及当在代码中使用DataContext时,例如(Main.xmal.cs)

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)

推荐答案

作为最佳实践,您可以在XAML,并在各方面的代码中获取它.

这是示例代码.您可以在XAML中(在UserControl.Resources中)实例化您的类,然后将数据上下文绑定到静态资源.

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.

<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>


这篇关于我们可以在Silverlight 4中使用数据上下文的几种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆