views:

10500

answers:

6

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

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>
Muad'Dib
Doesn't work. There needs to be a path somehow that says where HPVM is. in the code method, that what the "this." part does. HPVM is a property on the class. I cannot figure out the right way to reference the instantiated class to get to HPVM.
MattSlay
I added more code in the original question.
MattSlay
The second code example does work, but *creates* the object in XAML. It still does not address accessing constructor-created object instances from XAML. Best I can tell it cannot be done. You must set DataContext from code if object is created in code.
MattSlay
A: 

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

You have to do it through code.

Srdjan Jovcic
+3  A: 

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>

Klinger
If I instantiate the ViewModel class in XAML, can I still reference in the code-behind constructor? The reason I ask is that I presently set some values on the ViewModel in the constructor before the form is shown the user.
MattSlay
Yes, you can. You can do as follow: var aCustomer = this.Resources["Cust"] as Customer; aCustomer.Name = "abc";
Klinger
I forgot to add a x:Name attribute.x:Key works within XAML and x:Name makes the object visible to code.
Klinger
Yes, it does work, but *creates* the object in XAML. It still does not address accessing constructor-created object instances from XAML. Best I can tell it cannot be done. You must set DataContext from code if object is created in code.
MattSlay
I agree with you, it cannot be done. There is no syntax to supportthat.
Klinger
Instantiating object in XAML works if you are ok in using the Resources[".."] way.Using x:Name to expose the resource will not work(null reference) because the autogenerated class doesnot retrieves the object correctly (IMO).See my post here: http://silverlight.net/forums/t/77509.aspx
Klinger
A: 
<UserControl.DataContext>
  <vm:ThisUCViewModel />
</UserControl.DataContext>
someguy
+1  A: 

The following monstrosity works in Silverlight 4

<UserControl 
  DataContext="{Binding HPVM, RelativeSource={RelativeSource Self}}">
Chui Tey
A: 

good morning, un have blem when i work with de data grid view, whene i recover my data from the web sevice WCF, i wish display it in the grid view using the proprietes itemsouce for grid view but i have problem with binihg of data.

Lamou