Question

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.

Was it helpful?

Solution

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>

OTHER TIPS

The following monstrosity works in Silverlight 4

<UserControl 
  DataContext="{Binding HPVM, RelativeSource={RelativeSource Self}}">
<UserControl.DataContext>
  <vm:ThisUCViewModel />
</UserControl.DataContext>

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>

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.

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.

<UserControl.Resources>
  <ResourceDictionary>
     <vm:YourModelx:Key="myModel"/>
  </ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
   <Binding Source="{StaticResource myModel}"/>
</UserControl.DataContext>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top