Silverlight - 在 XAML 中而不是在构造函数中设置 DataContext? [英] Silverlight - Setting DataContext in XAML rather than in constructor?

查看:19
本文介绍了Silverlight - 在 XAML 中而不是在构造函数中设置 DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 XAML 中而不是在构造函数中在我的网格上设置 DataContext?

这是我在构造函数中的做法(LayoutRoot 是 XAML 中定义的 XAML 网格):

this.LayoutRoot.DataContext = this.HPVM;

我更喜欢在 XAML 中正确执行此操作,但我不知道如何在 XAML 中引用 HPVM 对象.HPVM 是 USerControl 类的公共属性.

上面列出的工作正常,但同样,我只想知道如何在 XAML 中设置 UserControl 类的属性,而不是总是必须在代码中进行.

这是所有相关代码:

 <UserControl.Resources>...

这是我当前设置 DataContext 的构造函数:

命名空间 SilverlightApplication1{公共部分类 SLHolePattern : UserControl, INotifyPropertyChanged{公共 HolePatternsViewModel HPVM;公共 SLHolePattern(){初始化组件();this.HPVM=new HolePatternsViewModel();this.LayoutRoot.DataContext = this.HPVM;...更多代码在这里}

一切正常,但我只想学习如何在 XAML 中设置 DataContext,而不是在代码中.

解决方案

Chris 给出的答案效果很好.我已经测试过了,它对我有用.您可以在 XAML(在 UserControl.Resources 内)中实例化您的类,并且然后将数据上下文绑定到静态资源.

关注代码:

<前><代码><用户控制...><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></网格></用户控件>

How can I set the DataContext on my Grid in XAML, instead of in the constructor?

Here is how I do it in the constructor (LayoutRoot is the XAML Grid defined in the XAML):

this.LayoutRoot.DataContext = this.HPVM;

I would prefer to do it right in the XAML, but I do not know how to reference the HPVM object in XAML. HPVM is a public property on the USerControl class.

It works fine as listed above, but again, I just want to know how to properties of the UserControl class in XAML, rather than always having to do it in code.

Here is all the relevant code:

  <UserControl x:Class="SilverlightApplication1.SLHolePattern" x:Name="HolePatternsControl"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib"    
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
    xmlns:local="clr-namespace:SilverlightApplication1"    
    xmlns:GeoPatterns="clr-namespace:GeoPatterns"
    Height="700">


    <UserControl.Resources>
    ...

And here is my constructor where the DataContext is currently set:

namespace SilverlightApplication1
{
    public partial class SLHolePattern : UserControl, INotifyPropertyChanged
    {
        public HolePatternsViewModel HPVM;

        public SLHolePattern()
        {
            InitializeComponent();

            this.HPVM=new HolePatternsViewModel();
            this.LayoutRoot.DataContext = this.HPVM;
            ...more code here
        }

It all works fine, but I just want to learn how to set the DataContext in XAML, not in code.

解决方案

The answer Chris gave works just fine. I have tested and it worked for me. You can instantiate your class in XAML (within the UserControl.Resources) and then bind the datacontext to a static resource.

Follow code:


<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 - 在 XAML 中而不是在构造函数中设置 DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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