Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - using DataContext to bind from Page linked by Frame

 
 
Old 04-23-2007   #1 (permalink)
buzzweetman@gmail.com


 
 

using DataContext to bind from Page linked by Frame

I have a window with a grid. In my C# window constructor, I set my
DataContext to a specific object instance where my data is.
Like this:
this.DataContext = myData;

Since I set that DataContext in code, all I have to do in the xaml is
bind to the path I want relative to my DataContext.

For example:
Text="{Binding Path=Title, UpdateSourceTrigger=PropertyChanged}"
....where Title is a property in myData.

This works fine for most of my controls.

But one of my controls is a data grid (the freebie from Xceed).
If I put the xaml for this control inline in my xaml, it works fine
with the binding:
ItemsSource="{Binding Path=MyTable}"
(so this refers to myData.MyTable)

The data grid shows the data from MyTable.

But i wanted to put the chunk of xaml that makes up this data grid in
a separate xaml file.
I surrounded it with the <Page> element and use <Frame> in my window
xaml to refer to it.
The data grid shows up, so I think I am "linking" to the new xaml file
properly, but it isn't binding properly. The data grid is empty...
which is what happens when I screw up the binding path.
I am using the same binding path as before:
ItemsSource="{Binding Path=MyTable}"

Clearly I am assuming the DataContext from my window xaml
automatically becomes the DataContext for the Page it refers to. But
I guess this isn't right.

What is the proper binding syntax to use in my new Page?

Thanks for any insight.
Buzz


My System SpecsSystem Spec
Old 04-24-2007   #2 (permalink)
Laurent Bugnion, MVP


 
 

Re: using DataContext to bind from Page linked by Frame

Hi,

buzzweetman@gmail.com wrote:
> I have a window with a grid. In my C# window constructor, I set my
> DataContext to a specific object instance where my data is.
> Like this:
> this.DataContext = myData;
>
> Since I set that DataContext in code, all I have to do in the xaml is
> bind to the path I want relative to my DataContext.
>
> For example:
> Text="{Binding Path=Title, UpdateSourceTrigger=PropertyChanged}"
> ...where Title is a property in myData.
>
> This works fine for most of my controls.
>
> But one of my controls is a data grid (the freebie from Xceed).
> If I put the xaml for this control inline in my xaml, it works fine
> with the binding:
> ItemsSource="{Binding Path=MyTable}"
> (so this refers to myData.MyTable)
>
> The data grid shows the data from MyTable.
>
> But i wanted to put the chunk of xaml that makes up this data grid in
> a separate xaml file.
> I surrounded it with the <Page> element and use <Frame> in my window
> xaml to refer to it.
> The data grid shows up, so I think I am "linking" to the new xaml file
> properly, but it isn't binding properly. The data grid is empty...
> which is what happens when I screw up the binding path.
> I am using the same binding path as before:
> ItemsSource="{Binding Path=MyTable}"
>
> Clearly I am assuming the DataContext from my window xaml
> automatically becomes the DataContext for the Page it refers to. But
> I guess this isn't right.
>
> What is the proper binding syntax to use in my new Page?
>
> Thanks for any insight.
> Buzz


I think that the Page will not have the same DataContext than the
Window, even though it is added to it. The Frame element is quite a
clear boundary, and I believe that it applies to the DataContext too.

Why not rather define the "chunk" of XAML defining the datagrid in a
ResourceDictionary, and the refer to that resource in your code. That
would be more conventional (if one can talk about conventional in WPF
;-) and the datacontext would clearly be the Window's.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Using DataContext with WCF services .NET General
templates linked from a page? Live Mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46