![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Using DataContext with WCF services | .NET General | |||
| templates linked from a page? | Live Mail | |||