Silverlight - Setting DataContext in XAML rather than in constructor?

走远了吗. 提交于 2019-11-26 13:07:17

问题


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.


回答1:


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>




回答2:


The following monstrosity works in Silverlight 4

<UserControl 
  DataContext="{Binding HPVM, RelativeSource={RelativeSource Self}}">



回答3:


<UserControl.DataContext>
  <vm:ThisUCViewModel />
</UserControl.DataContext>



回答4:


try something like this.....

<Grid DataContext="{Binding Path=HPVM}">
</Grid>

where HPVM is a public member of this--> your form etc.

Create the instance of your class in the xaml, by adding something like this to your resources section.... (don't forget to add your xmlns namespace)

<my:bogart x:Key="franken"/>

then, bind the data context to the static resource you just added....

<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource franken}">
    <TextBox  Background="Red" Foreground="White" Text="{Binding Path=sum}"  />
</Grid>



回答5:


In Silverlight 4, I was able to get this working by doing the following:

Give the Page/UserControl an x:Name="myPage"

In your control binding use normal Element bidning syntax. In my case I want to bind to an observable collection of objects in my code behind for my ItemsSource property:

<ComboBox 
    ItemsSource={Binding ElementName=myPage, Path=MyObservableObjectList, Mode=TwoWay}

I haven't tried this with DataContext but know you can do element to element binding for DataContext as I do this for Grids whose context is based on the selected item of some other drop down on the page.




回答6:


This is not possible (It is possible in WPF with {Binding RelativeSource={RelativeSource Self}}, but Silverlight is more limited.

You have to do it through code.




回答7:


<UserControl.Resources>
  <ResourceDictionary>
     <vm:YourModelx:Key="myModel"/>
  </ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
   <Binding Source="{StaticResource myModel}"/>
</UserControl.DataContext>


来源:https://stackoverflow.com/questions/606803/silverlight-setting-datacontext-in-xaml-rather-than-in-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!